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

Dear Monk,

i want to fetch the elements of array, which contain hashes, using reference. i have written the following code but there are errors. Would you help me with my code.
#!/usr/bin/perl -w use strict; my @arr; my %one; my %sec; my %thr; %one = { 1 => 'mili', 2 => 'brown', 3 => 'kalu' }; %sec = { 'age' => 23, 'eyes' => 'hazel', 'weight' => 128 }; %thr = { 'name' => 'deep', 'roll' => 02, 'class' => 'BI' }; my $one = \%one; my $sec = \%sec; my $thr = \%thr; @arr = ($one, $sec, $thr); my $ref = \@arr; for (my $i = 0; $i < 3; $i++) { my $a = $$ref[$i]; print "$a"; foreach my $x (keys %{$a}) { print $x; } } exit 0;
Thank You

Replies are listed 'Best First'.
Re: array of hashes
by bart (Canon) on Feb 10, 2011 at 12:37 UTC
    %one = { 1 => 'mili', 2 => 'brown', 3 => 'kalu' };
    Wrong. You should decide on whether you want to use a hash, in which case you must use normal parens; or a hash ref, in which case you can the curly braces. Thus, choose between:
    %one = ( 1 => 'mili', 2 => 'brown', 3 => 'kalu' );
    and
    $one = { 1 => 'mili', 2 => 'brown', 3 => 'kalu' };
    Note that %one and $one are not the same variable, and you use them differently. So you have to adapt the rest of your code to this choice.

    I see you've made this same mistake several times in your code.

      It would also be wise for a novice Anonymonk to use diagnostics; in addition to both strict and warnings in his or her code:

      >perl -wMstrict -le "use diagnostics; ;; my %hash = { foo => 'bar' }; " Reference found where even-sized list expected at -e line 1 (#1) (W misc) You gave a single reference where Perl was expecting a list with an even number of elements (for assignment to a hash). This usually means that you used the anon hash constructor when you meant to use parens. In any case, a hash requires key/value pairs. %hash = { one => 1, two => 2, }; # WRONG %hash = [ qw/ an anon array / ]; # WRONG %hash = ( one => 1, two => 2, ); # right %hash = qw( one 1 two 2 ); # also fine
Re: array of hashes
by moritz (Cardinal) on Feb 10, 2011 at 10:57 UTC
Re: array of hashes
by hsinclai (Deacon) on Feb 10, 2011 at 16:59 UTC
    #!/usr/bin/perl -w use strict; my $one = { 1 => 'mili', 2 => 'brown', 3 => 'kalu' }; my $sec = { 'age' => 23, 'eyes' => 'hazel', 'weight' => 128 }; my $thr = { 'name' => 'deep', 'roll' => 02, 'class' => 'BI' }; my @arr = ($one, $sec, $thr); my $ref = \@arr; for (my $i = 0; $i < 3; $i++) { while ( my($k,$v) = each %{$$ref[$i]} ) { print " $k " . '-> ' . $v . $/; } }
    There must be a nicer way to lay out the dereferencing, someone anyone?
      >perl -wMstrict -le "# as before... ;; my $arrayref = [ $one, $sec, $thr, ]; ;; for my $hashref (@$arrayref) { while (my ($k, $v) = each %$hashref) { print qq{ '$k' -> '$v'}; } } " '1' -> 'mili' '3' -> 'kalu' '2' -> 'brown' 'weight' -> '128' 'eyes' -> 'hazel' 'age' -> '23' 'name' -> 'deep' 'class' -> 'BI' 'roll' -> '2'