in reply to Hash of Arrays of Hashes Parsing

How about
foreach my $key (keys %foo) { if ($key eq "Info") { print "I'm working with the Info array\n"; print "Table name is 'Info'\n"; ### each element of the Info array is a hash reference ### foreach my $info_hashref (@{$foo{'Info'}}) { while (my ($k, $v) = each %$info_hashref) { print "column name is $k, value is $v\n"; } } } else: print "Table name is 'VIN'\n"; while (my ($k, $v) = each %{$foo{'VIN'}}) { print "column name is $k, value is $v\n"; } } }
Careful 'cause this is completely untested.

HTH.

Replies are listed 'Best First'.
Re: Re: Hash of Arrays of Hashes Parsing
by SmokeyB (Scribe) on Sep 19, 2003 at 15:12 UTC
    Hey hmerrill,

    Thank-you, that would work fine, but it's very specific. The hash itself can be populated with a wide variety of keys and data, never really know exactly what will be returned at the time.

    What I ended up doing is the following:
    print "\t<tr>\n"; if (ref($data) eq 'ARRAY') { foreach my $array (@$data) { foreach my $value (keys %$array) { print "\t\t<td>$array->{$value}</td>\n"; } } } elsif ref($data) eq 'HASH') { foreach my $value (keys %$data) { print "\t\t<td>$data->{$value}</td>\n"; } } else { print "\t\t<td>Too Bad Sucka!</td>\n"; } print "\t</tr>\n";
    Cheers!
      Your solution looks fine, but I have to comment on one thing - the naming of your variables. This is just personal preference, but IMHO it makes your code much clearer and much more readable if you name the variables - especially references - what they are, or what they contain.

      Here is your example with some of the variables renamed:
      print "\t<tr>\n"; if (ref($data) eq 'ARRAY') { ### Since each element of the array is a hash reference, ### name it so you know what it is. #foreach my $array (@$data) foreach my $hashref (@$data) { #foreach my $value (keys %$array) foreach my($key,$value) (each %$hashref) { #print "\t\t<td>$array->{$value}</td>\n"; print "\t\t<td>$value</td>\n"; } } } elsif ref($data) eq 'HASH') { #foreach my $value (keys %$data) foreach my($key,$value) (each %$data) { #print "\t\t<td>$array->{$value}</td>\n"; print "\t\t<td>$value</td>\n"; } } else { print "\t\t<td>Too Bad Sucka!</td>\n"; } print "\t</tr>\n";