Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Using Arrays with Hash key values

by JohnTabor (Novice)
on Oct 03, 2013 at 00:34 UTC ( #1056696=perlquestion: print w/replies, xml ) Need Help??
JohnTabor has asked for the wisdom of the Perl Monks concerning the following question:

Hi Im fairly new to perl and I am pretty stumped on a problem that I am having with hashes and trying to access a value of a key when I assign the value to be an array filled with stuff. I try to print the key and the value it returns is some crazy random numbers an integers like 3hx0jeh5. Please help

print "Enter the value"; my $item_to_push = <STDIN>; my @array = ('derp'); push(@array, $item_to_push); print @array; my %names; $names{'bill'} = [@array]; print $names{'bill'};

As you see I create a hash called %names, I assign a key in it called 'bill' to @array ( so that it is its value ), and I then try to print the value of the key 'bill' but this is what comes up. ARRAY(0x81c1f0)

Replies are listed 'Best First'.
Re: Using Arrays with Hash key values
by toolic (Bishop) on Oct 03, 2013 at 00:58 UTC
    Now that you have updated your post and added your code, you need to deference the array:
    use warnings; use strict; my $item_to_push = 'foo'; my @array = ('derp'); push(@array, $item_to_push); my %names; $names{'bill'} = [@array]; print "@{ $names{'bill'} }"; print "\n"; __END__ derp foo

    See also tip #4 from the Basic debugging checklist: Data::Dumper

      Ah ok I see thanks for the help its much appreciated!

Re: Using Arrays with Hash key values
by toolic (Bishop) on Oct 03, 2013 at 00:38 UTC

      Thanks for the help

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056696]
Approved by toolic
Front-paged by toolic
[erix]: "AngloZionist Empire"? "Stop the Empire's war on Russia?" WTF?
erix stops reading as recommended by the blog itself
[erix]: my attention being jealously guarded ;)
[shmem]: erix, yes, it's a WTF. Since...
[erix]: antisemitic && (pro-)russian. Makes sense :P
[shmem]: erix: what's antisemitic in there? Tell me.
[erix]: AngloZionist
[shmem]: and it is *not* pro-russian. It describes the western world.
[erix]: I'm not going to read it so I could be mistaken but it reeks of useful idiocy
[shmem]: AngloZionist is not an antisemitic term.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2018-03-19 20:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.