if(!exists $result{$uniqueKey}) # If this combinaison is unknown { $result{$uniqueKey} = [\%currentRow]; # save the current row to it } else { push @{ $result{$uniqueKey} }, \%currentRow; # add the current row to the existing ones } #### c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump; ;; my @ra = qw(foo a bar w bar x foo b bar y); dd \@ra; ;; my %hash; while (@ra >= 2) { my $k = shift @ra; my $v = shift @ra; ;; push @{ $hash{$k} }, $v } dd \%hash; ;; @ra = qw(foo A bar W bar X foo B bar Y); dd \@ra; ;; my $hashref; while (@ra >= 2) { my ($k, $v) = splice @ra, 0, 2; push @{ $hashref->{$k} }, $v; } dd $hashref; " ["foo", "a", "bar", "w", "bar", "x", "foo", "b", "bar", "y"] { bar => ["w", "x", "y"], foo => ["a", "b"] } ["foo", "A", "bar", "W", "bar", "X", "foo", "B", "bar", "Y"] { bar => ["W", "X", "Y"], foo => ["A", "B"] }