0

I have wrote the below routine, to iterate through hashes 0 - 7 and print out the value of a specific key in each. I need to grab the value of 'b4' in each hash.

I would like to do away with the (0..7), with something smarter for when there are different numbers of hashes. For instance, sometimes there is only 2 or there may be 160.

my $out = decode_json $client->responseContent();

#print "\nOutput is :\n\n" . Dumper $out;
for my $slice (0..7) {
    my $out = $out->{data}[$slice]{b4};
    print " $out \n";
}

The data is structured as such:

  DB<1> x $out
0  HASH(0x125fb5e0)
   'data' => ARRAY(0x1260d760)
      0  HASH(0x121765d0)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
      1  HASH(0x125fb650)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
      2  HASH(0x1236b960)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
      3  HASH(0x12177030)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
      4  HASH(0x1260da00)
            'b1' => '21'
            'b2' => '22'
            'b3' => '23'
            'b4' => '24'
            'b5' => '25'
            'b6' => '26'
            'b7' => '27'
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
lollan
  • 71
  • 5
  • 1
    [perldsc](http://perldoc.perl.org/perldsc.html) (the Perl data structures cookbook) might be helpful. It shows how to create and iterate through various data structures, like arrays of hashes. – ThisSuitIsBlackNot Mar 01 '16 at 21:59

2 Answers2

3

Your $out is a reference to a single-element hash which has an array reference for the value of its data element

It's best to extract the reference into a separate variable so that you can access the contents more simply. Suppose you wrote

my $data = $out->{data};

Thereafter, the array is accessible as @$data, the number of elements it contains is scalar @$data, and the indices are 0 .. $#$data. You can access each element of the array with $data->[0], $data->[1] etc.

Your code would become

my $out  = decode_json $client->responseContent;
my $data = $out->{data};

for my $i ( 0 .. $#$data ) {
    my $item = $data->[$i];
    my $b4 = $item->{b4};
    print "$b4\n";
}

But note that, unless you need the array index for other purposes, you are probably better off iterating over the array elements themselves rather than its indices. This code would do the same thing

my $out  = decode_json $client->responseContent;
my $data = $out->{data};

for my $item ( @$data ) {
    my $b4 = $item->{b4};
    print "$b4\n";
}

Or even just

print "$_->{b4}\n" for @$data;

if you don't need to do anything else within your loop

Borodin
  • 126,100
  • 9
  • 70
  • 144
0

Here's how to iterate over the array

for my $cur (@{$out->{data}})
{
    ...
}
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190