Collecting specific values from hash reference

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

  1. Hi,
    I have a hash reference as follows:

    $VAR1 = {
    A.processor0.error => 0
    A.processor0.success => 77
    A.processor0.total => 77
    A.processor1.error => 0
    A.processor1.success => 57
    A.processor1.total => 57
    A.processor2.error => 0
    A.processor2.success => 110
    A.processor2.total => 110
    }

    I want to collect the values from the different processors(for exp:
    success value of processor0,processor1,processor2 and sum them i.e 77
    + 57 +110 = 244 ).and store in a variable $success.

    If ($success == 244)
    {
    return 1;
    } else {
    return 0;
    }

    Can anybody help me in this regard?

    Regards
    Pradeep
    Pradeep Patra, Jan 21, 2012
    #1
    1. Advertising

  2. Pradeep Patra

    Tim McDaniel Guest

    In article
    <>,
    Pradeep Patra <> wrote:
    >Hi,
    > I have a hash reference as follows:
    >
    >$VAR1 = {
    > A.processor0.error => 0
    > A.processor0.success => 77
    > A.processor0.total => 77
    > A.processor1.error => 0
    > A.processor1.success => 57
    > A.processor1.total => 57
    > A.processor2.error => 0
    > A.processor2.success => 110
    > A.processor2.total => 110
    >}
    >
    >I want to collect the values from the different processors


    Then I think you have chosen a data structure that is not well-suited
    for your problem -- that you would be better off having a hash
    subscripted by processor0 or processor1 or processor2, the value of
    which is itself a has subscripted by error or success or total.
    If you do that, that would make the later collecting much easier.

    --
    Tim McDaniel,
    Tim McDaniel, Jan 22, 2012
    #2
    1. Advertising

  3. my $sum;
    $sum += $VAR1->{$_} foreach grep /success/, keys %{$VAR1};
    print $sum;
    George Mpouras, Jan 23, 2012
    #3
    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. Lorna70
    Replies:
    0
    Views:
    1,336
    Lorna70
    Aug 25, 2009
  2. rp
    Replies:
    1
    Views:
    493
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    596
    David A. Black
    Jul 2, 2008
  4. Replies:
    14
    Views:
    229
    Tomi Häsä
    Jan 10, 2005
  5. DJ Stunks
    Replies:
    3
    Views:
    127
    DJ Stunks
    Jan 21, 2009
Loading...

Share This Page