in reply to Re: Hash of Arrays of Hashes Parsing
in thread Hash of Arrays of Hashes Parsing

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!

Replies are listed 'Best First'.
Re: Re: Re: Hash of Arrays of Hashes Parsing
by hmerrill (Friar) on Sep 19, 2003 at 21:08 UTC
    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";