in reply to importing all the values stored in an HoH into an array

This might seem extensive but the recursion will fetch you all elements within any given HOH into a single array structure. Not sure if this is exactly your requirement..

$hash =
    {
        total_counts => tot_counts,
        words => {
                       nw => 1,
                 }
    };

@finalarray = get_array($hash);
print "@finalarray";

sub get_array {
    my($hash) = @_;
    my $array = [];
    if(ref $hash eq 'HASH') {
        foreach (keys %$hash) {
            if(ref $hash->{$_} eq 'HASH') {
                push @{$array} , $_;
                push @{$array} , get_array($hash->{$_});
            }
            else {
                push @{$array} , $_;
                push @{$array} , $hash->{$_};
            }
        }
    }
    return @$array;
}
  • Comment on Re: importing all the values stored in an HoH into an array

Replies are listed 'Best First'.
Re^2: importing all the values stored in an HoH into an array
by chinamox (Scribe) on Oct 31, 2006 at 13:45 UTC

    Thank you very much for the powerful bit of code. It didn't quite work for this problem, but I will certainly stick it in my pocket and use it in the future! Do you keep a library of handy code or did you just spin this off just now? In either case, Thank you very much for your time and efforts.

    -mox