Home > Software design >  How to print an element from an array which is inside the hash in perl
How to print an element from an array which is inside the hash in perl

Time:07-07

I'm trying to print the outputs from an API which are in multidimensional format.

use strict;
use warnings;
use Data::Dumper;

my $content={
  'school_set' => 'SSET1234',
  'result' => [
    {
      'school_name' => 'school_abc',
      'display_value' => 'IL25',
      'school_link' => 'example.com',
      'status' => 'registerd',
      'status_message' => 'only arts',
      'school_id' => '58c388d40596191f',
    }
  ],
  'school_table' => 'arts_schools'
};

print "school_name is=".$content{result}[0]{school_name};
print "school_status is=".$content{result}[3]{status};
output
Global symbol "%content" requires explicit package name (did you forget to declare "my %content"?) at test8.pl line 20.
Global symbol "%content" requires explicit package name (did you forget to declare "my %content"?) at test8.pl line 21.

I have to print the outputs like below from the result.

school_name = school_abc
school_status = registered

CodePudding user response:

If $content is a hash reference, you need to dereference it first. Use the arrow operator for that:

$content->{result}[0]{school_name}

The syntax without the arrow is only possible for %content.

my %content = ( result => [ { school_name => 'abc' } ] );
print $content{result}[0]{school_name};

If you want to print all the results, you have to loop over the array somehow. For example

#!/usr/bin/perl
use warnings;
use strict;

my $content = {
    'result' => [
        {
            'school_name' => 'school_abc',
            'status' => 'registerd',
        },
        {
            'school_name' => 'school_def',
            'status' => 'pending',
        }
    ],
};

for my $school (@{ $content->{result} }) {
    print "school_name is $school->{school_name}, status is $school->{status}\n";

}

CodePudding user response:

Your data structure assumes an array, perhaps it would be useful to utilize loop output for the data of interest.

The data presented as hash reference and will require de-referencing to loop through an array.

Following code snippet is based on your posted code and demonstrates how desired output can be achieved.

use strict;
use warnings;
use feature 'say';

my $dataset = {
              'school_set' => 'SSET1234',
              'result' => [
                {
                  'school_name' => 'school_abc',
                  'display_value' => 'IL25',
                  'school_link' => 'example.com',
                  'status' => 'registerd',
                  'status_message' => 'only arts',
                  'school_id' => '58c388d40596191f',
                }
              ],
              'school_table' => 'arts_schools'
            };

for my $item ( @{$dataset->{result}} ) {
    say "school_name is   = $item->{school_name}\n"
      . "school_status is = $item->{status}";
}

exit 0;

Output

school_name is   = school_abc
school_status is = registerd
  •  Tags:  
  • perl
  • Related