Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > foreach loops and lexically scoped loop variables

Reply
Thread Tools

foreach loops and lexically scoped loop variables

 
 
Derek Basch
Guest
Posts: n/a
 
      05-26-2006
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

 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      05-26-2006
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."


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      05-26-2006
Derek Basch <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      05-26-2006

"Derek Basch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.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


 
Reply With Quote
 
Derek Basch
Guest
Posts: n/a
 
      05-26-2006
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

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-27-2006
>>>>> "DB" == Derek Basch <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-29-2006
On Fri, 26 May 2006 23:11:21 -0400, Uri Guttman <(E-Mail Removed)>
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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
globs, ref globs, and lexically scoped filehandles Alex Hart Perl Misc 1 01-24-2005 11:02 PM
globs, ref globs, and lexically scoped filehandles Alex Hart Perl Misc 0 01-24-2005 08:21 AM
Lexically scoped variables Eric Sunshine Ruby 6 12-26-2003 10:55 AM
bug in lexically scoped array not reset in foreach loop Gavin Sherlock Perl Misc 1 12-10-2003 10:36 PM



Advertisments