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

Re: Perl hashes: is there some way of adding more data to the value associated with an existing key?

by wind (Priest)
on Jun 07, 2011 at 15:54 UTC ( [id://908513]=note: print w/replies, xml ) Need Help??


in reply to Perl hashes: is there some way of adding more data to the value associated with an existing key?

Just use a hash of arrays.
use strict; use warnings; my %data; while (<DATA>) { chomp; my ($k, $v) = split; push @{$data{$k}}, $v; } use Data::Dumper; print Dumper(\%data); __DATA__ 1 A 1 B 2 B 4 X 5 V
  • Comment on Re: Perl hashes: is there some way of adding more data to the value associated with an existing key?
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl hashes: is there some way of adding more data to the value associated with an existing key?
by sundialsvc4 (Abbot) on Jun 07, 2011 at 19:54 UTC

    Note:   Just to be absolutely clear about what this is ... it is a hash of array references.   (“Arrayrefs.”)

    The thing that is in a particular slot, of an array or of a hash or of a list, is always “a single value,” but one of the possible “values” that it can be, is “a reference to” absolutely anything else.   This is the magic of Perl data structuring, and one of the hallmark features of the language.   It allows arbitrary (and efficient!) data structures to be built out of a handful of simple primitives.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-18 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found