Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Two files from one.

by ellem (Hermit)
on May 22, 2002 at 14:37 UTC ( [id://168439]=perlquestion: print w/replies, xml ) Need Help??

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

I have a .txt document with the following format:

15. What is the average airspeed velocity of a sparrow?
a. 1,000,000
b. African or European
c. With or without a coconut
d. Your Mother smells of elderberries
Answer: d

What I'd like to do is make two separate files:
One like this:
15. What is the average airspeed velocity of a sparrow?
a. 1,000,000
b. African or European
c. With or without a coconut
d. Your Mother smells of elderberries

One like this:
15. Answer: d

The idea being that I want to separate the Answers and the Questions. I can't seem to get started properly. I understand I need to match certain words and that I need to hold the Question number in a variable for a moment while I get the answer so I can put them together but I am getting confused on the process. Not so much on the code.

How would you do this? (Psuedo code welcomed)
--
ellem@optonline.net
There's more than one way to do it, but only some of them actually work.

Replies are listed 'Best First'.
(shockme) Re: Two files from one.
by shockme (Chaplain) on May 22, 2002 at 15:24 UTC
    Just off the top of my head, here's some untested code that should get you started down the right path.
    while ($Input = <IN>) { if ($Input !~ /^Answer/) { if ($Input =~ /^(\d+)/) { $Number = $1; } print QUESTION "$Input"; } else { print ANSWER "$Number $Input"; } }

    If things get any worse, I'll have to ask you to stop helping me.

Re: Two files from one.
by Molt (Chaplain) on May 22, 2002 at 14:43 UTC

    Okay, I'd attack this with a few regexps. I'd have two output files concurrently open, one to answers.txt and one to questions.txt (or whatever), and simply while(<>) through the input file..

    The first regexp checks if the line starts with a series of digits, if it does then assume it's the question number and store it for later.

    The second regexp checks if the line begins with the word 'Answer', if it does then it prints out question number and this line to answers.txt, otherwise it prints out current line to questions.txt.

    Not quite pseudocode, but should be enough to go on!

Re: Two files from one.
by buckaduck (Chaplain) on May 22, 2002 at 16:38 UTC
    (Untested) If the questions in the input are separated by blank lines:
    # Read input a paragraph at a time # and put carriage returns on output lines local $/ = "\n\n"; local $\ = "\n"; # Read input, one question at a time while (<>) { chomp; # Grab the question number my $number = /^(\d+\.)/; # Split the question into lines my @question = split /\n/; # The answer is the last line my $answer = pop @question; # Send the results to different output streams print OUT1 join("\n", @question); print OUT2 "$number $answer"; }

    buckaduck

Re: Two files from one.
by cfreak (Chaplain) on May 22, 2002 at 16:33 UTC
    #untested open(QUESTIONS,">$questions") or die $!; open(ANSWERS,">$answers") or die $!; open(READ_FILE,"$read_file) or die $!; my($answer,$number); while(<READ_FILE>) { if($_ =~ /^(\d+)/) { $number = $1; } if($_ =~ /^Answer: (\w)/) { $answer = $1; # print the last number found print ANSWERS "$number. Answer: $answer\n"; } else { print QUESTIONS $_; } } close(ANSWERS); close(QUESTIONS); close(READ_FILE);

    Some clever or funny quote here.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-19 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found