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

well, i was just messing around with psuedo genetic programming, and had 30 arrays of the possible characters in a larger, and needed a rating for each one, so i decided i would either have the array of 30 possible character arrays, and another array that contained the rating for each item, i.e $rating[0] would be the rating for $possiblechar[0] (which is an array). I thought i could avoid the problem of making sure they were sync-ed by having a hash with the reference to the each array of possible characters be the key, and the pair for that key be the rating. That way there wouldn't be any problem making sure they were sync-ed or dealing with more then one data structure. Maybe not the smartest way outside of the fact that perl doesnt allow you to use array references as hash keys
  • Comment on Re: (Ovid) Re: array reference as hash key

Replies are listed 'Best First'.
(jeffa) 3Re: array reference as hash key
by jeffa (Bishop) on Feb 26, 2002 at 04:52 UTC
    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)
    
      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!
Re: Re: (Ovid) Re: array reference as hash key
by rjray (Chaplain) on Feb 26, 2002 at 04:16 UTC

    As I pointed out in an earlier message in this thread, you could use the references as keys, provided you have a secondary way of reverting the keys back into valid references.

    However, I think you would be better served by designing a more appropriate data structure for this task. Have a closer look at the Perl Data Structures page, or the Perl Data Structures Cookbook page.

    --rjray