use strict; #### Always have these lines. use warnings; #### They will save you lots of time debugging use DBI; use Time::localtime; use File::Compare; use XML::Simple; # qw(:strict); use Data::Dumper; my $user = "213256"; my @filesToProcess = glob "/export/home/$user/Tests/Match/dummy2*"; #### Need ""s. ''s do not do variable interpolation foreach my $file (@filesToProcess) { open(FILE, $file) or die "Can't open `$file': $!"; my $prev_var = -1_000_000; #### set this to a value that will never appear as the numbers you need to check. #### (Obviously the 2-digit numbers must be in the range 0 to 99) while ( my $line = ) #### do NOT read entire files into memory if they are big. Much better to process them a line at a time { chomp $line; my $var = substr($line, 10, 2); #### Do you mean substr($line, 9 , 2)? substr() consders the first character to be at offset zero if ($var != $prev_var) #### if number is different to number in previous line, then process it { $prev_var = $var; ... process $line here .... } } close FILE; } #### AB000000026JHAHKDFK AB00000003033AFSFAS = "30" line AB000000028JHKHKHKJ AB000000030HJHKH80J = "30" line AB0000000324446KJHK AB000000030LOIKJUJ8 = "30" line