my %map = (); while () { chomp $_; my @items = split(/\,/, $_); $map{$items[0]} = $items[1]; # [1] not [2] } close IN; my ($cur_awb,$cur_val); while () { chomp $_; if ($_ =~ m/\(\d*)\<\/KEY_BOL_REF\>/) { $cur_awb = $1; $cur_val = $map{$cur_awb}; # should do some error check here print OUT $_ . "\n"; } # ... etc. }