So, trying to put this together: It appears the combination of assigning the text to the variable ("@strings = <FILE>"), followed by the script, continuing to run (via the infinite loop), that can re-use the contents of @strings as many times as required, is the essence of the mechanism to avoid re-reading the file...
Can I assume that to be the case?
Edited to correct the typo... $strings
Thanks! | [reply] [Watch: Dir/Any] |
It's pretty common to see scripts process files one line at a time. So you have code like this:
open FILE, $filename or die "cant open: $!";
while(<FILE>) {
chomp;
print "read line: $_ \n";
}
close FILE;
This loop will iterate once for each line of the file. The instruction "Don't re-read the file..." means don't put a loop like this inside the infinite while(1) loop. Instead, you read all the lines (once) and save them in the @strings array. This is more efficient, because memory access is faster than file I/O.
| [reply] [Watch: Dir/Any] [d/l] |
So, trying to put this together: It appears the combination of assigning the text to the variable ("$strings = <FILE>"), followed by the script, continuing to run (via the infinite loop), that can re-use the contents of $strings as many times as required, is the essence of the mechanism to avoid re-reading the file...
Not really. For one thing you have a mistake (repeated twice) which may be a typo but which is also substantial, so I'm pointing it out: it's not $strings, but @strings! The difference is that the former on the lhs of an assignment imposes scalar context. Thus $strings = <FILE> puts into $strings a single "line". Now, I write "line" in double quotes because it may even be the whole file, as a single string, depending on the input record separator ($/ - look it up in perldoc perlvar). For simplity let's assume that the latter has not been changed from the default and that lines are actually lines: when you do @strings = <FILE> you're in list context instead and each element of the @strings array is a line. Then you iterate over it as over any other list. That's it.
Let's move on: perhaps a bigger and more severe misunderstanding on your part is with the infinite loop: that has nothing to do with looping over @strings, it is orthogonal. Indeed the latter is nested in the former: here you have two loops one within the other, the second of which disguised as a grep.
In all earnestness, I'm not familiar with the Llama book, but if this is its final exercise I must presume you've gone through all of it and please don't take it as a personal offense, but I find it a bit surprising that you're still doing all this confusion...
| [reply] [Watch: Dir/Any] [d/l] [select] |
I'm pointing it out: it's not $strings, but @strings
Thank you for identifying that error, I wasn't thinking while typing, and my typing, at its best, is, admittedly, horrendous! I do understand some of the basics of a loop nested within another loop, but my point was not meant to be that issue. I was reasoning that the design of the script, so as to avoid re-reading the original text file, should be such that, once the original file has been read (and its content stored in @strings), the script should continue running, awaiting further user input, with additional "patterns". Had it been written, for example, with a foreach loop, and terminated after each successful full pass through the elements of @strings, a user would then be obligated to re-run the script (and, hence, re-read the text file) to attempt another pattern.
I'm not familiar with the Llama book, but if this is its final exercise I must presume you've gone through all of it and please don't take it as a personal offense, but I find it a bit surprising that you're still doing all this confusion...
Regarding the "Llama book", it is actually "Learning Perl" by Randal L. Schwartz, et al, and published by O'Reilly Media, Inc. I have been trying to learn Perl, using this book for just 2 months, now, and I don't profess to know all of the information, therein. Do you, by chance, have a different text you would recommend for the purpose of learning Perl basics? If so, I would be willing to try it also.
Thanks, again, for your above comments!
| [reply] [Watch: Dir/Any] |