in reply to array in hashes

i have two hashes each of them contains an array for each key

No you don't.

First, there are no arrays anywhere in that code. Just lots of meaningless parens.

Secondly, hash values must be scalars, and arrays aren't scalars. You could store a reference to an array in a hash value, though.

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("Both genomes have key $_\n"); } }

Replies are listed 'Best First'.
Re^2: array in hashes
by persianswallow (Novice) on Jul 29, 2011 at 09:04 UTC

    i am sorry,i am new in perl.i got it,but if i wrote the following code ,because i need the values for each key, error is about arrays.i haven't gotten this!

    #!/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"); } }

      That code doesn't throw any errors. It might not give the output you want, but then you should at least tell us what output it produced, and what you wanted it to print instead.

      In this case reading perlreftut and applying what you learn there should solve your problem.

        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.