In article
<8fa546c3-4dcb-45ad-a9df->,
Pradeep Patra <> wrote:
> 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:

umper
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.
--
Jim Gibson