Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^5: Hard syntax error or disambiguable parsing?

by merlyn (Sage)
on Jan 29, 2009 at 05:41 UTC ( [id://739774]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Hard syntax error or disambiguable parsing?
in thread Hard syntax error or disambiguable parsing?

my $i = 5; for $i (10..15) { } print "$i\n";
prints 5. So clearly, the $i being iterated is not the $i from the my immediately above it.

A scalar member of an array is not a simple scalar. By Larry's rule, it has to be a simple scalar.

Replies are listed 'Best First'.
Re^6: Hard syntax error or disambiguable parsing?
by pobocks (Chaplain) on Jan 29, 2009 at 05:53 UTC

    Ahhhh... and he was enlightened. Basically, $i here is operated on as if it was:

    my $i; foreach local $i (0 .. 5){ ... }

    Do you have any ideas/pointers to the rationale for "no array members?" I understand that it is so, but fail to understand why it is so, when (for instance) one can local-ize individual array members.

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

      Nope, not that either. A Perl for loop variable is magical - there is no simple "equivalent" syntax that conveys what is going on under the hood. See my reply to your earlier node for (I hope) some real enlightenment.


      Perl's payment curve coincides with its learning curve.
Re^6: Hard syntax error or disambiguable parsing?
by ack (Deacon) on Jan 29, 2009 at 16:05 UTC

    I just tried the following code...which is a variation on what you show.

    #!/user/bin/perl use warnings; use strict; my $i = 10; print "$i\n"; foreach $i (0..5){ print "$i\n"; } # end foreach $i loop print "$i\n"; exit(0);

    It prints the following:

    10 0 1 2 3 4 5 10

    This is exactly what I believe the Camel says it should do since the loop variable is, if I recall correctly, always created anew as a lexical whose scope is the subsequent loop block (even though it appears to be created before the loop block). The output above seems to confirm that.

    I, like BrowserUk, however am a bit perplexed by the inability to use the $i[0] construct as the loop variable.

    Is it because the $i[0] implies a list structure which does not get created (via, for example, autovivification...spelling?)?

    ack Albuquerque, NM
      Actually, the loop variable will be lexical if there's a current lexical variable with that name, otherwise it will be local variable (with a local scope).
      my $i = 10; our $j = 10; sub print_it {say "[$i, $j]"} print_it; foreach $i (0 .. 5) {print_it;} print_it; foreach $j (0 .. 5) {print_it;} print_it; __END__ [10, 10] [10, 10] [10, 10] [10, 10] [10, 10] [10, 10] [10, 10] [10, 10] [10, 0] [10, 1] [10, 2] [10, 3] [10, 4] [10, 5] [10, 10]
      As you can see, $i is lexical, and its value isn't visible outside the loop. But $j is a package variable, who gets a localized value inside the loop. And then its value is visible outside the loop.
Re^6: Hard syntax error or disambiguable parsing?
by NateTut (Deacon) on Feb 13, 2009 at 21:26 UTC
    How could I get the for loop to use the predeclared $i instead of using a local one?
      How could I get the for loop to use the predeclared $i instead of using a local one?
      You don't, and that's a good thing. (You mean "foreach loop" there, I presume.)
        Well I have run into occasions where I would like use a previously declared variable as the loop variable in a situation like this. I know I can always just set a predeclared variable to be equal to the local loop variable, but that seems messier to me. Don't get me wrong I really like the local loop variable feature I would just like to "turn it off" occasionally. I probably don't understand some basic concept here most likely...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://739774]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found