![]() |
|
go ahead... be a heretic | |
PerlMonks |
Re: Handling lines and saving them into variablesby sundialsvc4 (Abbot) |
on Feb 15, 2011 at 15:11 UTC ( [id://888264]=note: print w/replies, xml ) | Need Help?? |
As you will undoubtedly hear, TMTOWTDI = There’s More Than One Way To Do It.” I rather like to do things as simply, and as obviously, as I can. The first thought that comes to mind is that you could use a loop which reads the input one string at a time, chomps it (to remove newlines), then appends it to a string variable. Now, within that loop, we have another while loop that tries to find question-marks within that string buffer. While it is able to do so, it grabs the first part of the buffer string (ending with the question-mark) as its output, then assigns the remainder of that string back to the string-buffer ... repeating this process until there are no more substrings left to be “eaten.” If the content of the string buffer contains no question marks, the inner while loop will of course execute zero times. At the end of these two nested while loops, the string buffer variable may be defined (but it won’t be, if the file was completely empty), and if so, it may contain a non-empty string, which is the detrius at the end of the file if the file didn’t end in a question-mark. I have decided not to provide code, because I think you want to figure it out on your own. “Use two nested while loops ...” “TMTOWTDI.”
In Section
Seekers of Perl Wisdom
|
|