Geetha Weerasooriya wrote:

I have an Array of Hashes   as follows:

@array = (
 { 'A'=>1, 'B' =>2, 'C'=>3, 'D'=>4}
{ 'A'=>5, 'B' =>6, 'C'=>7, 'D'=>8}
{ 'A'=>9, 'B' =>10, 'C'=>11, 'D'=>12}
{ 'A'=>13, 'B' =>14, 'C'=>15, 'D'=>16}
{ 'A'=>17, 'B' =>18, 'C'=>19, 'D'=>20}
{ 'A'=>21, 'B' =>22, 'C'=>23, 'D'=>24}
)

I have given a reference to this array in my script.

I want to do some calculation with only one key of each hash. For
example, I want to subtract from the value of key 'B' of last hash all
the other values of the same key into a one column like follows

22-2      20
22-6      16
22-10     12
22-14      8
22-18      4
22-22      0

Can you please tell me how to do this ?

Hi Geetha

I'm not sure exactly how you want to handle these values, so I've written some
code that pulls the values you need out into an array @b and reproduces from it
the output you describe. Hopefully you can use this to do what it is you want.

Rob



use strict;
use warnings;

my @array = (
 { 'A'=>1,  'B' =>2,  'C'=>3,  'D'=>4  },
 { 'A'=>5,  'B' =>6,  'C'=>7,  'D'=>8  },
 { 'A'=>9,  'B' =>10, 'C'=>11, 'D'=>12 },
 { 'A'=>13, 'B' =>14, 'C'=>15, 'D'=>16 },
 { 'A'=>17, 'B' =>18, 'C'=>19, 'D'=>20 },
 { 'A'=>21, 'B' =>22, 'C'=>23, 'D'=>24 },
);

my @b = map $_->{B}, @array;

foreach (@b) {
 printf "%-10s%2d\n", "$b[-1]-$_", $b[-1] - $_;
}

**OUTPUT**

22-2      20
22-6      16
22-10     12
22-14      8
22-18      4
22-22      0

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to