my %hash; foreach my $ary ( @array ) { if ( !defined($$ary[1]) ) { $$ary[1] = '0' } push @{$hash{$$ary[1]}}, $$ary[0]; } print Dumper \%hash;