Geetha Weerasooriya wrote:
> Hi ,
Hello,
> 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 ?
$ perl -le'
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 $ref = [EMAIL PROTECTED];
for my $hash ( @$ref ) {
print $ref->[ -1 ]{ B } - $hash->{ B };
}
'
20
16
12
8
4
0
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>