use warnings; use strict; use Data::Dumper; my @none; my %hash = map { chomp; local @_ = split; if (/\sNONE$/) { push(@none, $_[0]); (); } else { ($_[0] => $_[1]) }; } grep /^ID\d+/, ; warn Dumper \%hash, \@none; __DATA__ Script version 1.7 12345 hashes read from 1 database 3 read, 2 found, 2 written ID123 ZZ00000077 ID456 ZZ00000012 ID124 NONE #### :!perl test.pl $VAR1 = { 'ID456' => 'ZZ00000012', 'ID123' => 'ZZ00000077' }; $VAR2 = [ 'ID124' ];