chuck_norris has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, Is it possible to create a hash which maps an array to a string? If so, what is the syntax? Thanks, Chuck

Replies are listed 'Best First'.
Re: Mapping an array to a string
by moritz (Cardinal) on Jan 08, 2008 at 09:08 UTC
    What should that mapping looking like?

    You can do something like

    use Data::Dumper; my $string = Dumper \@array;

    Which represents an array as a string, but it doesn't use a hash (at least not visibly).

    Could you rephrase your question, and provide one or two examples what the result should look like?

Re: Mapping an array to a string
by Punitha (Priest) on Jan 08, 2008 at 09:45 UTC

    Hi chuck_norris

    Did you want to do something like this

    use strict; my @AoH = ( {'Lead'=>'fred','Friend'=>'barney'}, {'Lead'=>'george','Wife'=> 'jane','Son'=>'elroy'} ); my $string = \@AoH; for my $href ( @$string) { for my $role ( keys %$href ) { print "$role:$href->{$role}\n"; } }

    Punitha

Re: Mapping an array to a string
by olus (Curate) on Jan 08, 2008 at 14:09 UTC
    If I understood correctly
    use strict; use warnings; my @array=(1,2,3); my %hash = (); $hash{'string'} = \@array; print $hash{'string'}[0]; print $hash{'string'}[1]; print $hash{'string'}[2];
Re: Mapping an array to a string
by bunch (Sexton) on Jan 08, 2008 at 20:22 UTC
    Do you want to use arrays as hash keys? Only strings can be hash keys in Perl, but you can get a stringified representation of an array using eg. Data::Dumper or YAML, so you should do something like this:
    use Data::Dumper; $hash{Dumper(\@array)} = $value;