Collecting specific values from hash reference

P

Pradeep Patra

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
 
T

Tim McDaniel

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.
 
G

George Mpouras

my $sum;
$sum += $VAR1->{$_} foreach grep /success/, keys %{$VAR1};
print $sum;
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top