Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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:


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?

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 (Parson) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927749]
Approved by ww
[Lady_Aleena]: Right now, I'm trying to decide whether I want to rewrite how variables are entered in the sub I'm working on.
[shmem]: wait, what? you want that person to read the documentation for you?
[Lady_Aleena]: I might have to go with options instead of a straight list.
[Discipulus]: if more than 3 go for named variables LA
[Lady_Aleena]: Discipulus, it is at 2 now, but with what I am thinking about, it could go to 3. However, only 1 is needed. The second and third are optional.
[shmem]: straight list or named parameters - that depends on whether (and how many) optional arguments you have
[Discipulus]: if so a plain list is ok, imho
[Lady_Aleena]: shmem, I already have the plugin installed, just not active.
[shmem]: if you have 1 optional argument, place that at the end of the list. If you have more, go for named parameters.
[Lady_Aleena]: 1 manditory, 2 optional.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2017-04-27 12:19 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (506 votes). Check out past polls.