Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Don't re-read file for each new pattern...

by blazar (Canon)
on May 30, 2007 at 20:18 UTC ( #618308=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Don't re-read file for each new pattern...
in thread Don't re-read file for each new pattern...

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...


Comment on Re^3: Don't re-read file for each new pattern...
Select or Download Code
Re^4: Don't re-read file for each new pattern...
by cgmd (Beadle) on May 31, 2007 at 21:45 UTC
    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!

      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.

      Yes, this is almost a tautology: if it terminates after processing @strings once, then it will terminate and you would have to run the whole script once again. The point is that it caches file info into @strings and processes the latter each time for each supplied pattern.

      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.

      I know what the "Llama book" is, but I mean: I don't have it handy and I only had it a few times in my all life in my hands. I don't have any good recommendation because basically most of what I know stems from a week's reading of the first few chapters of a very old Llama (Perl 4 times I suspect) and then from clpmisc.

        The point is that it caches file info into @strings and processes the latter each time for each supplied pattern.

        That states it well!

        Thank you, again, for your help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://618308]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (15)
As of 2014-10-21 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (109 votes), past polls