in reply to parsing with two files

Assuming that file1 contains only code-ids and it is not huge in size.
use Data::Dumper; $filename = "codes.txt"; open $fh1, $filename or die "can't open $filename : $!\n"; $filename2= "./context.txt"; open $fh2, $filename2 or die "can't open $filename2 : $!\n"; chomp(@Ids=<$fh1>); LABEL: while(chomp(my $line = <$fh2>)){ foreach my $codeid (@Ids){ if($line =~ /-$codeid-/){ s/Event id -(\d+)-/$data{$codeid}=$1/e && next LABEL while +(<$fh2>); } } } print Dumper \%data;
the shown code will work and may be effective than yours(because I have not run benchmark on this).

Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.