use strict; use warnings; my %hash1 = ( ABC123 => '', ABC456 => '' ); my %hash2 = ( ABC456 => ''); my ( @out1, @out2 ); while ( my $entry = ) { chomp $entry; if ( exists $hash1{$entry} ) { push @out1, $entry; } if ( exists $hash2{$entry} ) { push @out2, $entry; next; } push @out1, $entry; } print "\nOUTPUT1:\n"; print "$_\n" for @out1; print "\nOUTPUT2:\n"; print "$_\n" for @out2; __DATA__ ABC123 ABC456 XYZ123 XYZ456 #### use strict; use warnings; my %hash1 = ( ABC123 => '', ABC456 => '' ); my %hash2 = ( ABC456 => ''); my ( @out1, @out2 ); while ( my $entry = ) { chomp $entry; my $seen; if ( exists $hash1{$entry} ) { push @out1, $entry; $seen = 1; } if ( exists $hash2{$entry} ) { push @out2, $entry; next; } push @out1, $entry unless $seen; } print "\nOUTPUT1:\n"; print "$_\n" for @out1; print "\nOUTPUT2:\n"; print "$_\n" for @out2; __DATA__ ABC123 ABC456 XYZ123 XYZ456