foreach loops and lexically scoped loop variables

Discussion in 'Perl Misc' started by Derek Basch, May 26, 2006.

  1. Derek Basch

    Derek Basch Guest

    Why does this:

    foreach my($key) (sort(keys(%reactivate_account_ref))) {
    print "I should work...but I don't";
    }

    produce this:

    Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i

    I searched alot and I can't find a good answer except for my Perl
    version being outdated and mine is not:

    perl -v

    This is perl, v5.8.3 built for x86_64-linux-thread-multii

    Thanks,
    Derek Basch
     
    Derek Basch, May 26, 2006
    #1
    1. Advertising

  2. Derek Basch

    Dr.Ruud Guest

    Derek Basch schreef:

    > foreach my($key) (sort(keys(%reactivate_account_ref))) {
    > print "I should work...but I don't";
    > }
    >
    > produce this:
    >
    > Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i


    $ perl -MO=Deparse -e '
    foreach my($key) (sort(keys(%reactivate_account_ref))) {
    print "I should work...but I don-t";
    }
    '
    Missing $ on loop variable at -e line 2.


    Remove the () around $key:


    $ perl -MO=Deparse -e '
    foreach my $key (sort(keys(%reactivate_account_ref))) {
    print "I should work...but I don-t";
    }
    '
    foreach my $key (sort keys %reactivate_account_ref) {
    print 'I should work...but I don-t';
    }
    -e syntax OK

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, May 26, 2006
    #2
    1. Advertising

  3. Derek Basch <> wrote:
    > Why does this:
    >
    > foreach my($key) (sort(keys(%reactivate_account_ref))) {
    > print "I should work...but I don't";
    > }
    >
    > produce this:
    >
    > Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i



    Because you are attempting to use invalid syntax.

    foreach my $key (sort(keys(%reactivate_account_ref))) {


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 26, 2006
    #3
  4. Derek Basch

    Matt Garrish Guest

    "Derek Basch" <> wrote in message
    news:...
    > Why does this:
    >
    > foreach my($key) (sort(keys(%reactivate_account_ref))) {
    > print "I should work...but I don't";
    > }
    >
    > produce this:
    >
    > Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i
    >


    Because you can only assign to a single scalar not a list (or array or
    hash), which is what you are doing by wrapping $key in parentheses.

    foreach my $key (sort keys %reactive_account_ref) {

    Matt
     
    Matt Garrish, May 26, 2006
    #4
  5. Derek Basch

    Derek Basch Guest

    Thanks everyone,

    I guess I just find it confusing what to put in parentheses and what
    not to in Perl. For instance I see examples all the time the have none
    around 'keys' and other that do. There doesn't seem to be a consistency
    to it.

    Derek Basch
     
    Derek Basch, May 27, 2006
    #5
  6. Derek Basch

    Uri Guttman Guest

    >>>>> "DB" == Derek Basch <> writes:

    DB> I guess I just find it confusing what to put in parentheses and what
    DB> not to in Perl. For instance I see examples all the time the have none
    DB> around 'keys' and other that do. There doesn't seem to be a consistency
    DB> to it.

    perl is actually very consistant. you just don't understand the
    consistancies. the conditional of an if/while is always in parens. keys
    is a builtin function and can be called with/without parens just like
    all the other builtins. perl is consistantly flexibile there. now you
    still may need to use parens for precedence but that is another
    consistancy you need to learn.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 27, 2006
    #6
  7. On Fri, 26 May 2006 23:11:21 -0400, Uri Guttman <>
    wrote:

    >perl is actually very consistant. you just don't understand the


    While I agree with all the rest of this answer of yours, and I find
    Perl to be much more consistent than is often regarded by people who
    don't really know it, it is perhaps slightly exaggerated to call it
    "very consistent". We all know two key ingredients in its design have
    been magic and pragmatism, possibly at the expense of some
    consistency...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, May 29, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sunshine

    Lexically scoped variables

    Eric Sunshine, Dec 19, 2003, in forum: Ruby
    Replies:
    6
    Views:
    144
    Robert Klemme
    Dec 26, 2003
  2. Gavin Sherlock
    Replies:
    1
    Views:
    102
    Uri Guttman
    Dec 10, 2003
  3. Alex Hart
    Replies:
    0
    Views:
    92
    Alex Hart
    Jan 24, 2005
  4. Alex Hart
    Replies:
    1
    Views:
    106
    Anno Siegel
    Jan 24, 2005
  5. Peter Michaux
    Replies:
    1
    Views:
    98
    Peter Michaux
    Jun 1, 2007
Loading...

Share This Page