Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: multiple array as hash value per key

by davorg (Chancellor)
on Jul 02, 2009 at 05:51 UTC ( [id://776640]=note: print w/replies, xml ) Need Help??


in reply to mutiple array as hash value per key

The values in a hash can be any kind of scalar. To get the results that you want, each value would need to be a reference to an array, and each element in the referenced arrays would need to be another array reference.

%number = ( 23 => [ [ 'g1', 23, 24 ], [ 'g2', 34, 35 ] ], 24 => [ [ 'g3', 45, 56 ], [ 'g4', 36, 48 ] ], ); $grp = $number{23}[0][0];

Note the I've corrected your hash-creating brackets from {} to ().

For more details see perldsc.

--

See the Copyright notice on my home node.

Perl training courses

Replies are listed 'Best First'.
Re^2: mutiple array as hash value per key
by Anonymous Monk on Jul 02, 2009 at 06:44 UTC
    in case i have new set of data (new array) referring to same key,is it possible to push the array value to the hash? eg :
    new set is read from a file while ($line <>){ ($key,$value)=split(/,/,$line); #suppose #line have 23,['g8',23,45] push @{$numbers{23}},$value; }
    will this append the new array to existing key ?

      What you're looking for is probably more like:

      while (<>) { chomp; my ($key, @values) = split /,/, $_; push @{$numbers{$key}}, [ @values ]; }

      When playing with complex data structures, perldsc and Data::Dumper are your friend.

      --

      See the Copyright notice on my home node.

      Perl training courses

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-18 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found