use strict; use warnings; ### ### !!! ALWAYS use strict and warnings !!! ### print "$0 Begin at ", scalar localtime, "\n\n"; open my $hsome_numbers, '<', "File1.text" or die "cannot open file File1"; #File1 should be renamed to something like some_numbers! my @some_numbers = <$hsome_numbers>; #slurp it! close $hsome_numbers; #remember every element of the array now has a line ending at its end, so chomp! open my $hstring_file, '<', "File2.text" or die "cannot open file File2"; #File2 should be renamed to something like string_file??? #what about the content in string_file, you showed us 7 lines containing "IT"??? #maybe should only unique content be used??? #I decided to only use the unique elements and do a slurp! my @strings = <$hstring_file>; #remember every element of the array now has a line ending at its end, so chomp! my @uniq_strings; my %seen = (); foreach my $item (@strings) { chomp $item; push(@uniq_strings, $item) unless $seen{$item}++; } # resulting array is @uniq_strings; open my $houtput_files_array, '<', "File3.text" or die "cannot open file File3"; #File3 should be renamed to something like output_files_array! #File3 is an array of filenames, so slurp them! my @filenames = <$houtput_files_array>; close $houtput_files_array; #remember every element of the array now has a line ending at its end, so chomp! for my $filename (@filenames) { chomp $filename; open my $hout, '>', "${filename}.output" #918.output, 908.output or die "cannot open file ${filename}.output for writing"; #now you have an output file per line in the array #per line in file some_numbers you want to write the line SR--SD-IBV-sw-A and the line SR-<>-SD<>-IBV-sw<>-B for my $string (@uniq_strings) { #per (unique) line in line in string_file for my $number (@some_numbers) { #per line in some_numbers chomp $number; print {$hout} "SR-$string-SD$number-IBV-sw$filename-A\n"; print {$hout} "SR-$string-SD$number-IBV-sw$filename-B\n"; # What you can see here is, that names matter. use variable names coming from your domain, so you will understand your program better. # Never use anything like file1, file2, line1, line2, this is BAD style and does not reflect your problem! } } close $hout; print "...processed output file ${filename}.output\n"; } print "$0 Ended at ", scalar localtime, "\n\n"; 1;