in reply to Re: (Ovid) Re: array reference as hash key
in thread array reference as hash key

If you are only concerned with what array belongs to a particular rating (i.e., you supply the rating and want to know what sequence it is), then just reverse your logic: make the rating the key and the array the value.

If you are also concerned with what rating belongs to a particular sequence however (i.e., you supply the sequence and want to know it's rating), you will have to rely on a more complex data structure than an array or a hash. One example is an array reference that contains array references that contain the rating as their first element, and the sequence (as yet another array reference) as the second element:

my $ref = [ [2, [qw(g a t a c a)]], [3, [qw(a t a c a g)]], [1, [qw(t a c a g a)]], ];
And a nasty one liner to print them out (one per line) ordered by their rating:
print map {join('',@{$_->[1]})."\n"} sort {$a->[0] <=> $b->[0]} @$ref;

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) 3Re: array reference as hash key
by smgfc (Monk) on Feb 26, 2002 at 05:00 UTC
    in response to your comments and ovid's (thanks alot by the way) i pondered my options, I thought of using the rating as the key and the array as the value (jeffa's post), and using two seperate sync-ed arrays, one holding smaller arrays, the other each arrays rating, but i decided to do something, i think, simpiler, which fit into what i had already coded. I joined the array of characters, and made it the key (and the rating the value), and when i needed to use it i just split it back into an array. A little ugly, but i cant think of a solution i like more. If anyone has any more suggestions, that would be great anyway!