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

Re^6: hash & arrays

by fullermd (Priest)
on Nov 05, 2009 at 13:28 UTC ( #805260=note: print w/replies, xml ) Need Help??

in reply to Re^5: hash & arrays
in thread hash & arrays

Your example code isn't doing what you're saying it is...

my %hash = []; #explicitly create a hash of arrays structure for clari +ty

This doesn't mean anything, and is in fact an error; you can't assign a single scalar (an array reference in this case) to a hash.

#push your items into the array - this will create the hash $userid if + it dosent already exist push @{$hash{$userid}}, ($title, $adjst, $adjst2, @{$linex}) #@{$linex) says i am a list

$userid isn't a hash. %hash is. $hash{$userid} is a hash element. In saying @{$hash{$userid}} you're asking perl to treat that hash element as an array reference, and dereference it; in this case, the element doesn't exist, and so is created as what it ostensibly is (an array ref), and then used via push to push those elements onto it. The parenthesis are unnecessary and don't mean anything here.

Also, $linex in his code sounds like a string, not an array ref.

Replies are listed 'Best First'.
Re^7: hash & arrays
by Spooky (Beadle) on Nov 05, 2009 at 16:56 UTC
    OK, I coded the following which seems to work - although I'm quite sure why it works: my %hash ; $title = 'engineer' ; $num = 23.4 ; $userid = 'ewh1234' ; @linx2 = (1, 2, 3) ; $hash{$userid} = [$title, $num, $linx2[0], $linx21, $linx2]; print "@{$hash{$userid}}\n" ; print "$hash{$userid}4\n" ; which produces: engineer 23.4 1 2 3 3 I have to learn more about hashes versus arrays - thanks for your help...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://805260]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2020-11-30 05:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found