in reply to Re^3: array in hashes
in thread array in hashes

thank you for your helps.

#!/usr/bin/perl -w use strict; my %genome1 = ( a => [ 10, 15 ], b => [ 3, 7 ], c => [ 9, 14 ], ); my %genome2 = ( d => [ 3, 21 ], a => [ 3, 4 ], g => [ 5, 10 ], b => [ 2, 7 ], ); for (keys(%genome1)) { if ($genome2{$_}) { print("$genome1{$_} and $genome2{$_}\n"); } }

we have a in each of hashes (3,4) and (10,15).i want to calculate the slope of the line between (3,4) and (10,15), it means (15-4)/(10-3) or for b we have (2,7) and (3,7),i want (7-7)/(3-2) as output.m=(y2-y1)/(x2-x1).but i dont know how i have to define x and y for numbers in hashes.

Replies are listed 'Best First'.
Re^5: array in hashes
by moritz (Cardinal) on Jul 29, 2011 at 09:54 UTC
Re^5: array in hashes
by fisher (Priest) on Jul 29, 2011 at 10:07 UTC
    i want (7-7)/(3-2) as output.

    Is this what you want?

    for (keys(%genome1)) { if ($genome2{$_}) { print "for ".$_." it is (".${$genome1{$_}}[1]."-". ${$genome2{$_}}[1].")/(".${$genome1{$_}}[0]."-".${$genome2{$_}}[ +0].")\n"; } }
      There's no need for the additional blocks. Writing $genome1{$_}[1] instead of ${$genome1{$_}}[1] is just fine.

        Further to JavaFan's point about clarity, there's also no need to go overboard using concatenation for complex hash or array values in a string. They will interpolate just fine:

        >perl -wMstrict -le "my %hash = ( a => [11,22], b => [33,44], ); my $hashref = { c => [55,66], d => [77,88], }; ;; print qq{$hash{a}[0] $hash{b}[1] $hashref->{d}[1]}; " 11 44 88
        Yes, I know. I wrote this for clarity.

      thank you soooooooooooooo much.:-) i need amount of those calculation.i mean (15-4)/(10-3)is equal to 1.5714286 but it doesnt print that.