Help for this page
while(my $job_B=readdir(DIR_B)){ ... next unless $job_B =~ /Out_$tag\.txt$/; }
next unless $job_B =~ /Out_$tag\.txt$/; #.... until you have checked t +he whole directory and then goto: beginning.