For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2006 > foreach loops and lexically scoped loop variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author foreach loops and lexically scoped loop variables
Derek Basch

2006-05-26, 7:02 pm

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

Tad McClellan

2006-05-26, 7:02 pm

Derek Basch <dbasch@yahoo.com> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Dr.Ruud

2006-05-26, 7:02 pm

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."


Derek Basch

2006-05-26, 7:02 pm

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

Matt Garrish

2006-05-26, 7:02 pm


"Derek Basch" <dbasch@yahoo.com> wrote in message
news:1148679901.193531.26520@i40g2000cwc.googlegroups.com...
> 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


Uri Guttman

2006-05-26, 10:00 pm

>>>>> "DB" == Derek Basch <dbasch@yahoo.com> 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 ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Michele Dondi

2006-05-29, 4:07 am

On Fri, 26 May 2006 23:11:21 -0400, Uri Guttman <uri@stemsystems.com>
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{po
p^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,
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com