Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Searching Complex Array Values

Reply
Thread Tools

Searching Complex Array Values

 
 
Pradeep Patra
Guest
Posts: n/a
 
      01-15-2012
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
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      01-17-2012
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
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      01-18-2012
In article <170120121100027068%>, Jim Gibson
<> wrote:


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


Sorry for the typo. That should be:

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

--
Jim Gibson
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      01-18-2012
On Sat, 14 Jan 2012 22:31:38 -0800 (PST) Pradeep Patra <> wrote:

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
Searching within the hash values of an array of hashes usenet@DavidFilmer.com Perl Misc 1 08-26-2007 04:55 AM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57