http://www.perlmonks.org?node_id=400696


in reply to Making a hash of arrays from a datafile

use strict; use warnings; my %hash; m/(\d+),(\d+)/ and push(@{$hash{$1}}, $2) for <DATA>; for (sort keys %hash) { print "$_ => "; print "$_ " for sort @{$hash{$_}}; print "\n"; } __DATA__ 2,6 2,3 1,2 2,5 1,3 1,4
This creates a hash with the first value as key and all the second values pushed into nested arrays. Sorting and printing is simple, as seen above.