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

Hi there!
I am looping this array but I am getting "HASH(0x28c0af8)" as part of the results.
Any better way?
$VAR1 = { '12349' => [ { 'name' => 'RIC', 'reference' => '00y77676', } ], 'X334' => [ { 'name' => 'MES', 'reference' => '0089kkjh9', } ], 'U7564' => [ { 'name' => 'CHAMV', 'reference' => '00gt6544', } ], 'Q234' => [ { 'name' => 'INGTON', 'reference' => '0099ytr', } ], 'K988777' => [ { 'name' => 'LOE', 'reference' => '9887hhh', }, { 'name' => 'TYL', 'reference' => '00ttr54433', }, { 'name' => 'TLYN', 'reference' => '099iuuy7', }, { 'name' => 'LEEN', 'reference' => '0yy76767', }, { 'name' => 'TSY', 'reference' => '00hyu65', }, { 'name' => 'ASTCIA', 'reference' => '0dree4333', }, { 'name' => 'IRNDA', 'reference' => '99877uuug', } ], 'P99987' => [ { 'name' => 'JOSPH', 'reference' => '0gft5210', } ], }; process(\%thedata); sub process { my $all = shift; my %run_all = %$all; foreach my $key (keys %run_all){ print "$key\n"; foreach my $entry (@{$run_all{$key}}) { print "$entry\n"; } print "\n"; } }

Thank you!

Replies are listed 'Best First'.
Re: Get values from array
by bigj (Monk) on Apr 21, 2014 at 19:52 UTC
    Well, your entries are hash (refs), so no wonder that
    print "$entry\n";
    prints HASH(reference). Replace it e.g. with access to the hash like
    print " Name: ", $entry->{name}, " Reference: ", $entry->{reference}, "\n";
    and it results in:
    K988777 Name: LOE Reference: 9887hhh Name: TYL Reference: 00ttr54433 Name: TLYN Reference: 099iuuy7 Name: LEEN Reference: 0yy76767 Name: TSY Reference: 00hyu65 Name: ASTCIA Reference: 0dree4333 Name: IRNDA Reference: 99877uuug U7564 Name: CHAMV Reference: 00gt6544 X334 Name: MES Reference: 0089kkjh9 P99987 Name: JOSPH Reference: 0gft5210 Q234 Name: INGTON Reference: 0099ytr 12349 Name: RIC Reference: 00y77676

    Greetings,
    Janek Schleicher

Re: Get values from array
by chilledham (Friar) on Apr 21, 2014 at 19:55 UTC

    You are seeing HASH(0x28c0af8) because $entry is a reference to a hash. Notice the curly braces around the key-value pairs in your data structure:

    { 'name' => 'JOSPH', 'reference' => '0gft5210', }

    You'll want to deference that inner hash.

    sub process { my $all = shift; my %run_all = %$all; foreach my $key (keys %run_all){ print "$key\n"; foreach my $entry (@{$run_all{$key}}) { while (my ($k, $v) = each %$entry) { print "$k => $v\n"; } } print "\n"; } }

Re: Get values from array
by davido (Cardinal) on Apr 21, 2014 at 20:21 UTC

    Look at it this way:

    $VAR1 = { '12349' => [ { 'name' => 'RIC', 'reference' => '00y77676', } ], 'X334' => [ { 'name' => 'MES', 'reference' => '0089kkjh9', } ], }; my $href = $VAR1->{12349}[0]; print $href, "\n";

    What would you want print to do, if handed a hashref? What it does, is it stringifies the reference, and that's what you are seeing.

    print "$_ => $VAR1->{12349}[0]{$_}\n" for keys %{$VAR1->{12349}[0]};

    ...will probably get you a little closer to what you want. Your structure is three levels deep. You're not penetrating it deeply enough.


    Dave

Re: Get values from array
by mhearse (Chaplain) on Apr 21, 2014 at 21:13 UTC
    You have a hashref, whose value is an anonymous array. The elements within the anonymous array are anonymous hashrefs.... Man Perl is cool.

    while (my ($key, $value) = each %{$thedata}) { # $value is anon arrayref for my $hashref (@{$value}) { print $hashref->{name}, "\n"; print $hashref->{reference}, "\n"; } }
Re: Get values from array
by Anonymous Monk on Apr 22, 2014 at 04:09 UTC

    Or you could use a for loop with a map instead of nested for loops:

    sub process { my %run_all = %{ shift(@_) }; foreach my $key ( keys %run_all ) { print "$key\n", map { join( "\t" => @$_{ (qw[name reference]) } ), $/ } @{ $run_all{$key} } } }

Re: Get values from array
by Laurent_R (Canon) on Apr 21, 2014 at 21:33 UTC
    Just another way to look at it:
    foreach my $key (keys %$VAR1) { print "$key\n"; foreach my $valref (@{$VAR1->{$key}}) { print "\t Name = $valref->{name}\n"; print "\t Reference = $valref->{reference}\n\n"; } }
    which will print the following: Or, if you care only about the final data, something somewhat simpler:
    foreach my $vals (values %$VAR1) { foreach my $valref (@$vals) { print "Name = $valref->{name}\n"; print "Reference = $valref->{reference}\n\n"; } }
    which prints the following: