Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Array loops and assignments

by davido (Archbishop)
on Jul 08, 2013 at 01:26 UTC ( #1043023=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Array loops and assignments
in thread Array loops and assignments

Definitely overcomplicating it. Try this idiom: Think of a hash as a namespace, and its elements as variables within that namespace.

my @array = qw( Larry Curly Moe ); my %stooges; @stooges{ @array } = ( 0 .. $#array ); print "$_ = $stooges{$_}\n" for keys %stooges;

Or if you want numbers in the name:

%stooges = map { 'var' . $_ , $array[$_] } 0 .. $#array;

Dave


Comment on Re^3: Array loops and assignments
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2015-07-28 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls