The code you've provided does what it's supposed to do.
First of all, you are opening the file myfile , reading it completely and then closing the associated filehandle, once for each line in the file dummy_file .
For every line from dummy_file in which the 2nd field $F[1] (split on white-space using the -a run-time switch) matches the pattern patt anchored at the end, the first element of the list slice (split(" ", $i))[0] , obtained by splitting the line from myfile on white-space, is displayed.
What exactly are you trying to do? Please elaborate with input and output samples.
Thank you, but the dummy file is just an empty file to fulfill or complete the syntax (else Perl reads from stdin). The actual file in action is "myfile" where I am looping over its contents by reading from the file handle 'F'. The white space splitting is done on the myfile's contents and not of the dummy file.
Ok, now I got the idea and I have revised my code as below:
perl -lane 'open F, "< f1"; for $i (<F>) {chomp $i; if ($i =~ /$F[1]$/) {my $f = (split(" ", $i))[0]; print "$f";}} close F' f2
I am printing here the 1st field of file f1 if any of its line contains file f2's second column's string as a pattern to be matched at the end of the line
The input contents are:
$ cat f1
aa b c patt
11 2 3 4
$ cat f2
This patt
1234 xxxx
Now, it's working like a charm and I am getting the expected result
Why would perl complain when what you were doing was syntactically correct but logically wrong?
Never use a for (or foreach) loop to read a file. In this case, all lines from the file (depending on the input record separator) will be read in at once and stored in memory to form a list. Your loop will then iterate over the elements of this list. So, you might hit your system's memory limits.
A while loop is much much better as it reads one record at a time.