Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Handling lines and saving them into variables

by sundialsvc4 (Abbot)
on Feb 15, 2011 at 15:11 UTC ( [id://888264]=note: print w/replies, xml ) Need Help??


in reply to Handling lines and saving them into variables

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2025-06-24 04:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.