Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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
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 (Priest) 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 (Canon) 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 perusing the Monastery: (6)
As of 2014-07-29 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls