Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: variable like $_

by fullermd (Priest)
on Feb 04, 2009 at 06:10 UTC ( [id://741187]=note: print w/replies, xml ) Need Help??


in reply to variable like $_

There are potentially ways by doing really deep trickery, but why would you bother? Just use assigned variables for each loop.

foreach my $k1 (keys %var) { foreach my $k2 (keys %{$var{$k1}}) { print('$var{$k1}{$k2}=', $var{$k1}{$k2}, "\n"); } }

Replies are listed 'Best First'.
Re^2: variable like $_
by Marshall (Canon) on Feb 04, 2009 at 12:29 UTC
    I like this post and looks good to me! There is no penalty for having a lexical "my" variable as an iterator in a "foreach" loop. Think about it, here $k1 and $k2 are separate things. Even if you succeed in writing this with just $_, you haven't saved anything, Perl will essentially create these vars even if you haven't given them names - they are separate things to Perl (inner $_ is separate from outer $_). Give them names! I agree!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found