Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Mapping an array to a string

by chuck_norris (Novice)
on Jan 08, 2008 at 08:59 UTC ( #661031=perlquestion: print w/replies, xml ) Need Help??

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://661031]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2020-07-03 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?