Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Accessing and printing an array, INSIDE of a hash

by perlpreben (Beadle)
on Sep 25, 2011 at 14:21 UTC ( #927749=perlquestion: print w/ replies, xml ) Need Help??
perlpreben has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have the following code

my @array = ("a","b","c","d","e"); my %hash; $hash{'key'} = @array;

This is all dandy, but when I try to print it, I only get the scalars out (number of values inside the array)

while (my ($key, @value) = each(%hash)){ my @extracted_array = @value; }

So how can I access the array inside of the hash for further manipulation?

Comment on Accessing and printing an array, INSIDE of a hash
Select or Download Code
Replies are listed 'Best First'.
Re: Accessing and printing an array, INSIDE of a hash
by Corion (Pope) on Sep 25, 2011 at 14:36 UTC

    This line:

    $hash{'key'} = @array;

    ... does not do what you think it does. Hashes can only take scalar values, not arrays. You can store a reference to the array:

    $hash{'key'} = \@array;

    ... and then retrieve it back. See Data::Dumper for inspecting your data structures, References Quick Reference on how to get at the data.

Re: Accessing and printing an array, INSIDE of a hash
by RichardK (Vicar) on Sep 25, 2011 at 14:40 UTC
    It's also worth having a look at the perldsc -- Perl Data Structures Cookbook
Re: Accessing and printing an array, INSIDE of a hash
by planetscape (Chancellor) on Sep 26, 2011 at 17:06 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2015-07-29 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls