Searching Complex Array Values

P

Pradeep Patra

Hi all,
I have a complex array reference. I want to search the
'name'="v1" and if it matches then search for "ctrs" and then 'C-data'
get the value of "success"?

$VAR1 = [
#0
{
'timestamp' => '1020929',
'inst' => [
#0
{
'A-data' => [
#0
{
'C' => [
#0
{

'B-data' => [

#0

{

'c-count' => '8',

'result' => 'complete'
}
]
}
],
'name' => 'v1',
'id' => 'v1',
'ctrs' => [
#0
{
'C-
data' => [

#0

{

'value' => '0',

'name' => 'success'
},

#1

{

'value' => '0',

'name' => 'error'
},

#2

{

'value' => '0',

'name' => 'percent'
},

#3

{

'value' => '0',

'name' => 'latency'
},

#4

{

'value' => '0',

'name' => 'total'
},

]
}
]
}
]
}
]
}
];

I am trying to retrieve as following But it doesn't work. Am I missing
anything in dereferencing I guess. A sample source code will help.

my $a = $VAR1[0]; ---> To get "inst"
my $b = $a->{'A-data'};
print "A is $a";
print "B is $b";
if ($b->{'name'} eq 'v1') {
print "PASS";

}

Is there a efficient way of searching this kind then it will be
useful?

Regards
Pradeep
 
J

Jim Gibson

Pradeep Patra said:
Hi all,
I have a complex array reference. I want to search the
'name'="v1" and if it matches then search for "ctrs" and then 'C-data'
get the value of "success"?

$VAR1 = [

{contents of $VAR1 snipped}
];

I am trying to retrieve as following But it doesn't work. Am I missing
anything in dereferencing I guess. A sample source code will help.
my $a = $VAR1[0]; ---> To get "inst"

Are you using 'use strict;' and 'use warnings:'? If you did, then you
would get the following error message from Perl for the above line:

"Global symbol "@VAR1" requires explicit package name at pradeep.pl
line 66.
pradeep.pl had compilation errors."

$VAR1 is a reference to an array, not an array, so the above line needs
to be changed to:

my $a = $VAR1->[0];
my $b = $a->{'A-data'};

$a is now a reference to a hash whose keys are 'timestamp' and 'inst'.
If you are looking for a hash element with key 'A-data', you are going
to have to go down two more levels:

my $b = $a->{inst}-=>[0]->{'A-data'};
print "A is $a";
print "B is $b";

$a is a reference to a hash, and $b is a reference to an array, so it
does little good to print them. You might want to use the Data::Dumper
module to print the contents of the data structures pointed to by $a
and $b.
if ($b->{'name'} eq 'v1') {
print "PASS";

}

Is there a efficient way of searching this kind then it will be
useful?

I would worry more about your logic at this point than efficiency. It
is really hard to tell what you are trying to do.

If you want more help, please post a complete program that demonstrates
what you are trying to do and describe why it does not match your
expectations. Please also keep your lines to a reasonable number of
columns.

Thanks.
 
T

Ted Zlatanov

PP> I have a complex array reference. I want to search the
PP> 'name'="v1" and if it matches then search for "ctrs" and then 'C-data'
PP> get the value of "success"?

....

PP> Is there a efficient way of searching this kind then it will be
PP> useful?

You may like the CPAN Data::Match module. It's built for this kind of
searching.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top