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

Re: Can I create hash dynamically by looping through an array? The hash name should be the array element

by choroba (Chancellor)
on Jan 25, 2013 at 12:51 UTC ( #1015340=note: print w/replies, xml ) Need Help??


in reply to Can I create hash dynamically by looping through an array? The hash name should be the array element

Yes, you can - if you are not using strict refs:
for my $element (qw/var1 var2 var3/) { %$element = ( a => 'b'); } print $var1{a};
The common practice, though, is to use one hash instead, making the elements of the list the keys. You can then shorten the creation to
my @array = qw(var1 var2 var3); my %hash; undef @hash{@array};
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: Can I create hash dynamically by looping through an array? The hash name should be the array element
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015340]
help
Chatterbox?
[perldigious]: I watched some video on YT awhile back with auto-subtitle on and the speaker had a very thick cockney sort of English accent... hillarity ensued in the subtitles.
[Discipulus]: IHAA=I hate acronyms anyway
LanX LOLs
[perldigious]: If you want a linguistic adventure...
[perldigious]: No offense to any Scotsman, I love Scots. Well actually, I love Scotch, but I'm sure the people are great too. :-P
[Discipulus]: perldigious i understand i word on ten, to be optimistic..

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2017-06-23 16:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (552 votes). Check out past polls.