Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by punch_card_don (Curate)
on Jan 25, 2013 at 14:19 UTC ( #1015353=note: print w/ replies, xml ) Need Help??


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

Record another vote for a hash of hashes.

#!/usr/bin/perl -w use strict; print "Content-type:text/html\n\n"; my @array = qw(var1 var2 var3); my %HoH = (); my $i = 0; foreach my $element (@array){ $i++; $HoH{$element} = { 'key'.$i.'1', 'value'.$i.'1', 'key'.$i.'2', 'va +lue'.$i.'2', 'key'.$i.'3', 'value'.$i.'3' }; } foreach my $key1 (sort keys %HoH){ foreach my $key2 (sort keys %{ $HoH{$key1} }){ print "<br>$key1 > $key2 > $HoH{$key1}{$key2}\n"; } }
Output:
var1 > key11 > value11 var1 > key12 > value12 var1 > key13 > value13 var2 > key21 > value21 var2 > key22 > value22 var2 > key23 > value23 var3 > key31 > value31 var3 > key32 > value32 var3 > key33 > value33



Time flies like an arrow. Fruit flies like a banana.


Comment on Re^2: 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://1015353]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2015-07-29 12:34 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 (263 votes), past polls