Searching Complex Array Values

Discussion in 'Perl Misc' started by Pradeep Patra, Jan 15, 2012.

  1. 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
     
    Pradeep Patra, Jan 15, 2012
    #1
    1. Advertising

  2. Pradeep Patra

    Jim Gibson Guest

    In article
    <>,
    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::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.

    --
    Jim Gibson
     
    Jim Gibson, Jan 17, 2012
    #2
    1. Advertising

  3. Pradeep Patra

    Jim Gibson Guest

    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
     
    Jim Gibson, Jan 18, 2012
    #3
  4. Pradeep Patra

    Ted Zlatanov Guest

    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
     
    Ted Zlatanov, Jan 18, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    602
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    784
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    716
    Robert Mark Bram
    Feb 4, 2007
  4. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    799
  5. stumblng.tumblr
    Replies:
    1
    Views:
    231
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page