Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Handling lines and saving them into variables

by mistamutt (Novice)
on Feb 15, 2011 at 00:11 UTC ( [id://888103]=perlquestion: print w/replies, xml ) Need Help??

mistamutt has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm new to Perl and I've been reading an intro book as well as a few of the guides and FAQs on this site as well as a couple of others. I have a txt file written with HTML containing questions and answers. I've so far been able to get the question with the answer on the same line, and after the answer to the question, for some reason, the end tags still remain. My question(s) are:

  • How do I save a string starting at the beginning of the line, until the question mark? Basically what I'm trying to do is store the question in a variable, then the answer in a separate one (which would start from the white space after the question mark). I've been reading and trying for a couple of days without much success, so if anyone could help me I'd greatly appreciate it
  • Comment on Handling lines and saving them into variables

Replies are listed 'Best First'.
Re: Handling lines and saving them into variables
by chromatic (Archbishop) on Feb 15, 2011 at 05:54 UTC

    Can you post some example data as well as the code you have?

Re: Handling lines and saving them into variables
by ciderpunx (Vicar) on Feb 15, 2011 at 00:58 UTC

      Thanks, sorry about the all caps Perl thing =P

Re: Handling lines and saving them into variables
by jeffa (Bishop) on Feb 15, 2011 at 20:50 UTC

    If your HTML is not terribly complex, you could use something like YAML

    use strict; use warnings; use Data::Dumper; use YAML; # Load a YAML stream of 3 YAML documents into Perl data structures. my $data = do {local $/ = <DATA> }; my @yaml = Load( $data ); for my $hash (@yaml) { my $question = $hash->{question}; my $answer = $hash->{answer}; print "q: $question\na: $answer\n"; } __DATA__ --- question: <b>What... is your name?</b> answer: <p>My name is Sir Lancelot of Camelot.</p> --- question: <b>What... is your quest?</b> answer: <p>To seek the Holy Grail.</p> --- question: <b>What... is your favourite colour?</b> answer: <p>Blue.</p>

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Handling lines and saving them into variables
by sundialsvc4 (Abbot) on Feb 15, 2011 at 15:11 UTC

    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: perlquestion [id://888103]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2024-07-21 03:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.