I've parsed JSON into the following data structure:
$VAR1 = {
'041012020' => {
'item_number' => 'P2345'
},
'041012021' => {
'item_number' => 'I0965'
},
'041012022' => {
'item_number' => 'R2204'
}
};
I'm trying to get the values of item_numbers using the following code, and it's giving me the HASH Values as output rather than the actual item_number values. Please guide me to get the expected values.
foreach my $value (values %{$json_obj}) {
say "Value is: $value,";
}
Output:
Value is: HASH(0x557ce4e2f3c0),
Value is: HASH(0x557ce4e4de18),
Value is: HASH(0x557ce4e4dcf8),
If I use the same code to get the keys it's working perfectly fine
foreach my $key (keys %{$json_obj}) {
say "Key is: $key,";
}
Output:
Key is: 041012020,
Key is: 041012020,
Key is: 041012022,
The values of the hash elements are references to hashes ( { item_number => 'P2345' }
). That's what you get when you stringify a reference. If you want the item number, you'll need to tell Perl that.
for my $value (values %$data) {
say $value->{item_number};
}
or
for my $item_number ( map { $_->{item_number} } values %$data ) {
say $item_number;
}
Here is the short code for your question.
#!usr/bin/perl $VAR1 = { '041012020' => { 'item_number' => 'P2345' }, '041012021' => { 'item_number' => 'I0965' }, '041012022' => { 'item_number' => 'R2204' } }; print "$VAR1->{$_}->{item_number}\n" for keys %$VAR1;
To use for in a block:
for my $key (keys %$VAR1) {
print "$VAR1->{$key}->{item_number}\n"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.