use List::Util qw/ min max sum /; $a = -55; $b = -54; $c = -55; $d = -54; for (1..110)# Start your loop { $file = "x_$a\_to_$b\_y$c\_to_$d";#file name open(INFILE,"<","$file") or die("Couldn't open file for reading!\n"); while(){ push @column8,(split(/\s+/,$_))[7];##Push data to array } if(@column8)## if array is not empty then it will go inside { $ZMAX = max @column8; print "maxZ $ZMAX\n"; $ZMIN = min @column8; print "minZ $ZMIN\n"; $thick = $ZMAX - $ZMIN; print "$thick\n"; } ## Once the values are printed this will help you to erase the previous values of $ZMAX, $ZMIN, $thick, @column8 for your second file $ZMAX=""; $ZMIN=""; $thick=""; @column8=(); $c++; $d++; ## Note this loop will run for 110 times so it seems you should have 110 files when you increment $c++ and $d++; }