while(defined ($file = readdir(DIR))) { next if $file =~ /^\.\.?$/; if ($file =~ /_s_/) { $file = "../human/chr".$c.".fa/$file"; foreach $tool (@$tools) { print $file,"\n"; # <------ line 1 $tool =~ s//$file/; # <------ line 2 print $tool,"\n"; # <------ line 3 } } }