Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't print keys and values in Perl

I have a data structure that I got from this code.

my $name = $data->{Instances}->[0]->{Tags};

That data structure looks like this

$VAR1 = [
      {
        'Key' => 'Name',
        'Value' => 'fl-demo'
      },
      {
        'Value' => 'FL',
        'Key' => 'state'
      }
    ];

I’m trying to print the keys and values with this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

foreach my $key (sort keys %$name) {
my $value = $name->{$key};
print "$key  => $value\n";
}

I’m getting

Not a HASH reference at ./x.pl line 19.

>Solution :

The tags are returned as a list, not a hash. So you’re looking at doing something like this, instead, to iterate over them:

foreach my $tag (@$name) {
    my $key = $tag->{Key};
    my $val = $tag->{Value};
    print "$key => $val\n";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading