in reply to array reference as hash key

Why would you want to use an array reference as a hash key? The point of a hash is to have a key to quickly and easily access data in a non-sequential manner. Using an array reference doesn't seem to facilitate that (of course, I'm not sure what you're trying to accomplish). If you want, you can take an array's reference and by using the stringified value as a hash key, you can get a one-to-one mapping, but this really strikes me as something to be avoided without good reason.

So, the question is, what problem are you trying to solve?

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: array reference as hash key
by smgfc (Monk) on Feb 26, 2002 at 02:05 UTC
    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
      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!

      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

Re^2: array reference as hash key
by Anonymous Monk on Jun 09, 2011 at 19:49 UTC

    I know this is an old thread, but I thought I'd answer the question (why would you want to use a reference as a hash key) anyway.

    In my case I want to use a hash reference as a key to another hash so I can link two different hashes together w/o duplicating the keys repeatedly in the other hash. I have a doubly linked hash (a->b and b<-a) and I also have large keys for each hash so I don't want to duplicate them repeatedly.

    Consider the case of processing student enrollment data. In one hash I want to link each occurrence of a student (the key) to the list of classes he took (the value: an anonymous hash w/ the class name as the key and the grade as the value). I also want to link the opposite way in another hash where the class name is the key and the value is an anonymous hash with the student name as key and the grade as the value. Since all my keys are large text strings (student name or class name), I would prefer to store a reference to the opposing hash rather than the key itself.