o2bwise has asked for the wisdom of the Perl Monks concerning the following question:



Hi,

I entered a question before, but am still stumped. I reduced what I think may be the source of my uncertainty to the following.
#!/usr/bin/perl use strict; use Data::Dumper; use regression; open (TEST,">xxx.txt"); my %test = (); my @teams = ('packers','lions','bears','vikings'); my @data = (5,10,15,20); my $counter = 1; foreach my $team (@teams) { my $counter2 = 0; foreach my $data_value (@data) { $test{$team}{'raw_data'}{$counter} = $data_value; print TEST "$team: $counter - $test{$team}{'raw_data'}{$counte +r}\n"; $data[$counter2] = $data[$counter2] * 2; $counter2++; $counter++; } } close (TEST);
Question 1:
Did the above create four hash data structures with the following key, value pairs?

Hash 1
1 -> 5,
2 -> 10,
3 -> 15,
4 ->20

Hash 2
5 -> 10,
6 -> 20,
7 -> 30,
8 -> 40

Hash 3
9 -> 20,
10 -> 40,
11 -> 60,
12 -> 80

Hash 4
13 -> 40,
14 -> 80,
15 -> 120,
16 -> 160

Question 2:
How do I print out each of these four hashes?

It’s a bit of a mimic of something for work. I need to pass what is for me a complicated hash into a small perl mod I made.

Tony

Replies are listed 'Best First'.
Re: difficulty with complicated (for me) hashes
by Tanktalus (Canon) on Dec 18, 2008 at 23:35 UTC

    Rather than answering your question, I'm going to help you answer your own question. Look at Data::Dumper. Add the following code to the end of your test script:

    use Data::Dumper; print Dumper(\%test); # printing a ref to a hash is easier to read tha +n printing the hash itself...
    Then look at the output (you may want to redirect to a file or pipe through less, if you have it). Hopefully that helps you understand what's going on.

      Awesome! Thank you and I am all set.

      o2 (Tony)
Re: difficulty with complicated (for me) hashes
by pat_mc (Pilgrim) on Dec 18, 2008 at 23:46 UTC
    Hi, 2bwise -

    To verify if key-value pairs of some %hash have been generated I typically use the following construct:
    for my $key ( keys %hash ) { print "$key \t $hash{ $key }"; }
    When using multidimensional hashes, things are only slightly complicated by the fact that the intermediate dimensions of your hash only return references. In those cases it is necessary to derefence the reference to obtain the actual hash.
    In your example this would be something like
    for my $key ( key %{ $test{$team}{'raw_data'} } ) {...}


    Hope this helps.

    Pat
      Thanks, Pat and I am all set! :-)

      o2
Re: difficulty with complicated (for me) hashes
by MarkovChain (Sexton) on Dec 19, 2008 at 01:03 UTC
    Tintin is at: /Users/Tintin/Documents/Programs/Perl/Perl_monks>cat -n +monk1.pl 1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 7 use Data::Dumper; 8 #use regression; 9 10 open (TEST,">xxx.txt"); 11 12 my %test = (); 13 14 my @teams = ('packers','lions','bears','vikings'); 15 my @data = (5,10,15,20); 16 17 my $counter = 1; 18 foreach my $team (@teams) 19 { 20 my $counter2 = 0; 21 foreach my $data_value (@data) { 22 $test{$team}{'raw_data'}{$counter} = $data_value; 23 print TEST "$team: $counter - $test{$team}{'raw_data +'}{$counter}\n"; 24 $data[$counter2] = $data[$counter2] * 2; 25 $counter2++; 26 $counter++; 27 } 28 } 29 30 local $\ = "\n"; # Set the output rec separator to newline 31 foreach my $team (keys %test ) { 32 foreach my $counter (keys %{$test{$team}{'raw_data'}} ) + { 33 print STDOUT $team . '{raw_data}' . $counter . ' val +ue is ' . $test{$team}{'raw_data'}{$counter}; 34 } 35 } 36 37 close (TEST); Tintin is at: /Users/Tintin/Documents/Programs/Perl/Perl_monks> Output is: Tintin is at: /Users/Tintin/Documents/Programs/Perl/Perl_monks>perl mo +nk1.pl lions{raw_data}8 value is 40 lions{raw_data}6 value is 20 lions{raw_data}7 value is 30 lions{raw_data}5 value is 10 packers{raw_data}4 value is 20 packers{raw_data}1 value is 5 packers{raw_data}3 value is 15 packers{raw_data}2 value is 10 bears{raw_data}11 value is 60 bears{raw_data}10 value is 40 bears{raw_data}9 value is 20 bears{raw_data}12 value is 80 vikings{raw_data}16 value is 160 vikings{raw_data}13 value is 40 vikings{raw_data}15 value is 120 vikings{raw_data}14 value is 80
    Cheers!!!!
      Cool! Thanks!

      Tony
Re: difficulty with complicated (for me) hashes
by u671296 (Sexton) on Dec 18, 2008 at 23:42 UTC
    No there's only one (nested) hash. %test To print a hash for debugging: print "@{%hash}\n"; from that you'll get back references to the contained hashes, so really you want to loop through the hash keys foreach my $key (keys %test) { my $ref = $test{$key}; } to get the references $ref, then dereference that foreach my $nextkey (keys %$ref) { my $nextref = $test{$key}{nextkey}; } As you know the depth of hashing you can avoid using the ref() function to determine if a reference or a scalar vale has been returned by $test{$key}.
      Oooops sorry the formatting screwed up. Yes Data::Dumper is probably more what you really need.
      Thanks, man! I'm good.

      Tony