in reply to How to dereference when hash key is referenece

#!/usr/local/bin/perl use strict; use warnings; my $array_ref = [1, 2, 5, 7, 4]; my %hash; $hash{join $;, @$array_ref} = 'ashok'; for my $as (keys %hash){ print join ',', split /$;/, $as; }

Prints:

1,2,5,7,4

as you wanted. This works by creating a string that contains the array values and uses the string as the hash key. The split pulls the key string back apart for printing.

This is very likely not a good solution to your actual problem however!


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: How to dereference when hash key is referenece
by codeacrobat (Chaplain) on Apr 27, 2006 at 05:52 UTC
    If in the x part of his question $array_ref is ment to store only integers then this is ok. You have a problem, when an array element contains the $; char and you convert string -> array(ref).

      I doubt very much that the question asked was the real question. To that extent any answer is bogus and refinements, while usefull for teaching in a broader sense, are not actually addressing OP's real problem.

      I consider that the code I posted as an answer to the question asked to be sub-optimum in many ways, but unless we can figure out what OP is trying to do, we probably can't do much better.


      DWIM is Perl's answer to Gödel