my (@output, @output1); for my $i (0 .. $#array) { if (exists $hash{ $array[$i] } and $array1[$i] == $hash{ $array[$i] }{highest}) { push @output, $array[$i]; push @output1, $array1[$i]; } else { push @output, ''; # or use spaces ' ' here push @output1, ''; # or whatever you want } }