Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by Anonymous Monk
on Jan 25, 2013 at 12:50 UTC ( #1015338=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

Are you quite certain you don't really want a Hash of Hashes?


Comment on Re: Can I create hash dynamically by looping through an array? The hash name should be the array element
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
    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2015-07-01 19:21 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 (17 votes), past polls