chomp $_; # where $_ is the current line from the original list my $prefix = $_; my $txtfile = $prefix . ".txt"; if (-f $txtfile){ do stuff } else{ $txtfile1 = $prefix . "1.txt"; # the # is always 1 do stuff }