## your original code @{$sets{$fasta_id}[$setscounter]{$sitekey}} = grep { $_ >= $lowerlimit and $_ <= $upperlimit } @{$matches{$fasta_id}{$sitekey}}; #### @{$sets{$fasta_id}[$setscounter]{$sitekey}} = (); foreach ( sort @{$matches{$fasta_id}{$sitekey}} ) { last if $_ > $upperlimit; next if $_ < $lowerlimit; push @{$sets{$fasta_id}[$setscounter]{$sitekey}}, $_; }