use strict; use warnings; use diagnostics; use Data::Dumper; my %hash; my @data = ("foo",1, "bar",2, "foo",3, "baz",4); while (my $key = shift @data) { my $value = shift @data; push @{$hash{$key}}, $value; # push the value on the array } print Dumper \%hash #### $VAR1 = { 'bar' => [ 2 ], 'baz' => [ 4 ], 'foo' => [ 1, 3 ] };