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

I am trying to print the hash values, but i am not getting the actual result instead array reference

$VAR1 = { 'A1' => { 'val1' => [23], 'val2' => [42], 'val3' => [15], 'val4' => [10] } B1' => { 'val1' => [13], 'val2' => [22], 'val3' => [25], 'val4' => [11] };
my %data; foreach my $key (sort keys %data) { foreach my $elem (keys %{$hash{$key} { print"$key : $hash{$key}->{$val}"; } }

but instead of actual values, its print ARRAY(00X)reference

Replies are listed 'Best First'.
Re: print hash elements
by AnomalousMonk (Archbishop) on Sep 12, 2016 at 19:43 UTC

    Using a reconstructed version of what may be your original data (with some extra variety thrown in for the contents of the bottom-level array references), maybe something like this:

    c:\@Work\Perl>perl -wMstrict -le "use Data::Dump qw(dd); ;; my %data = ( 'A1' => { 'val1' => [23], 'val2' => [42, 99], 'val3' => [15], 'val4' => [10 +], }, 'B1' => { 'val1' => [13], 'val2' => [], 'val3' => [25], 'val4' => [11], }, ); dd \%data; ;; for my $top_key (sort keys %data) { print qq{'$top_key':}; for my $sub_key (sort keys %{ $data{$top_key} }) { print qq{ '$sub_key': (@{ $data{$top_key}{$sub_key} })}; } } " { A1 => { val1 => [23], val2 => [42, 99], val3 => [15], val4 => [10] } +, B1 => { val1 => [13], val2 => [], val3 => [25], val4 => [11] }, } 'A1': 'val1': (23) 'val2': (42 99) 'val3': (15) 'val4': (10) 'B1': 'val1': (13) 'val2': () 'val3': (25) 'val4': (11)
    Please also see perldsc.


    Give a man a fish:  <%-{-{-{-<

Re: print hash elements
by GotToBTru (Prior) on Sep 12, 2016 at 18:45 UTC

    [22] is an array reference. Leave off the square brackets if you want the literal 22 as the value. This is a very valuable resource.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re^2: print hash elements
by perl-diddler (Chaplain) on Sep 13, 2016 at 13:16 UTC
    You could be very simplistic about it and use 'P' (a module in CPAN,
    also, after fixing some syntax errors in your VAR statement):
    > perl -we' use strict; use P; my $VAR1 = { "A1" => { "val1" => [23], "val2" => [42], "val3" => [15], + "val4" => [10] } ,"B1" => { "val1" => [13], "val2" => [22], "val3" = +> [25], "val4" => [11] }}; P "-----------\nvar=%s", $VAR1;' ----------- var={A1=>{val1=>[23], val2=>[42], val3=>[15], val4=>[10]}, B1=>{val1=> +[13], val2=>[22], val3=>[25], val4=>[11]}}
    But if you are wanting an introduction to references, you might look at the book "Intermediate Perl" by Schwartz, foy & Phoenix (on amazon), Chapter 4, "Introduction to References". It's very important to understand what you are doing in the code if you want to print it out...

    BTW -- what was "$hash" supposed to be in your 2nd line of your original code?

      ...or the in-core Data::Dumper...

      perl -wMstrict -MData::Dumper -E '...; print Dumper $VAR1'