in reply to How to open file inside loops with file names generated using an array?

Maybe the arrays don't contain what you think they should? Try printing the values to verify:
for my $dat1 (@data1) { for my $dat2 (@data2) { my $filename = "file_$dat1\_$dat2.txt"; print "<$filename>\n"; open my $fh, '<', $filename or die "a horrible death $!"; } }
Note the angle brackets around the filename, they should help you find potential whitespace. The most common mistake is a missing chomp.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: How to open file inside loops with file names generated using an array?
by skooma (Novice) on May 28, 2018 at 08:20 UTC
    I have tried that. The file names are printing properly. The variables themselves independently also looks fine when printed. Also I can open these files when I loop them directly using the array like.
    open ( TEST, ">", "test.txt") or die "\n$!\n"; foreach my $var ( @file_list ) { print "\n$var\n"; open ( RL, "<", $var ) or die "\n$!\n"; while (<RL>){ print TEST $_; } }

      skooma:

      You missed one bit that choroba put in his code: He wrapped the filename in brockets (<>) so that whitespace at the beginning or ending of the line would be more obvious. I expect that he's thinking your code would print something like:

      <foo >

      instead of the expected:

      <foo>

      because you may have gotten your data from a file without remembering to chomp off the line endings.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.