Hi in the debugger I print out the following variable $x
DB<8> x $x
0 ARRAY(0xdb09e20)
0 HASH(0xe18d450)
'OBJECT_ID' => 111026746749824
'TABLE_KEY' => '0-85296-384-X'
'TABLE_KEY_NAME' => 'VALUE'
1 HASH(0xe18d4e0)
'OBJECT_ID' => 1000000000108155
'TABLE_KEY' => '1598-8740'
'TABLE_KEY_NAME' => 'VALUE'
2 HASH(0xe18d530)
'OBJECT_ID' => 1000000000108156
'TABLE_KEY' => '89-7085-418-5'
'TABLE_KEY_NAME' => 'VALUE'
I would like to loop through this and access each OBJECT ID
I do not know what syntax to use.
Here is a dump of $x
[
{
OBJECT_ID => 111026746749824,
TABLE_KEY => "0-85296-384-X",
TABLE_KEY_NAME => "VALUE",
},
{
OBJECT_ID => 111026746749824,
TABLE_KEY => "0537-9987",
TABLE_KEY_NAME => "VALUE",
},
{
OBJECT_ID => 1000000000108155,
TABLE_KEY => "89-7085-386-3",
TABLE_KEY_NAME => "VALUE",
},
{
OBJECT_ID => 1000000000108155,
TABLE_KEY => "1598-8740",
TABLE_KEY_NAME => "VALUE",
},
{
OBJECT_ID => 1000000000108156,
TABLE_KEY => "89-7085-418-5",
TABLE_KEY_NAME => "VALUE",
},
{
OBJECT_ID => 1000000000108156,
TABLE_KEY => "1598-8651",
TABLE_KEY_NAME => "VALUE",
},
{
OBJECT_ID => 1000000000108157,
TABLE_KEY => "89-7085-442-8",
TABLE_KEY_NAME => "VALUE",
},
{
OBJECT_ID => 1000000000108157,
TABLE_KEY => "1598-8597",
TABLE_KEY_NAME => "VALUE",
},
]
It looks like you have an array reference that contains hash references as the elements.
I can tell that it's a an array ref because it's $x with a $ as the sigil, which indicates a reference, and because of the 0 ARRAY(0xdb09e20).
So if you print Dumper it using Data::Dumper it, you'll get something like this:
[
{
'OBJECT_ID' => 111026746749824,
'TABLE_KEY' => '0-85296-384-X',
'TABLE_KEY_NAME' => 'VALUE',
},
{
'OBJECT_ID' => 1000000000108155,
'TABLE_KEY' => '1598-8740',
'TABLE_KEY_NAME' => 'VALUE',
}
]
If you look at it in this presentation, it becomes pretty obvious what you need to do.
First, you iterate over the elements in the array reference, which you have to dereference. I'm using a very verbose dereferencing here because I think it's cleaner to read if you don't know exactly what you're doing. You could also do @$x.
foreach my $elem ( @{ $x } ) {
# ...
}
Now you have $elem which contains one of the hash references. To get the OBJECT_ID key, you need to access it. Because it's a ref, you need the dereferencing operator ->.
say $elem->{OBJECT_ID};
This will print out the OBJECT_ID for one element. say is like print with a newline. You can to load it with use feature 'say'.
It comes together like this:
use strict;
use warnings;
use feature 'say';
my $x = ...;
foreach my $elem ( @{ $x } ) {
say $elem->{OBJECT_ID};
}
It will print:
111026746749824
1000000000108155
To learn more about references in Perl, look at perlref and perlreftut. You can also look at these tutorials on Perlmaven:
This will do as you ask
my @ids = map { $_->{OBJECT_ID} } @$x;
say for @ids;
111026746749824
111026746749824
1000000000108155
1000000000108155
1000000000108156
1000000000108156
1000000000108157
1000000000108157
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With