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 locked_user 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.