CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Search Question and Print Answer in Text File

 



tqlam
New User

Jan 10, 2008, 1:28 PM

Post #1 of 1 (212 views)
Search Question and Print Answer in Text File Can't Post

I have a text file with Questions and Answers in below.
I wrote a script in perl to search a question(s), if match Q's, then print Answers up to End_Q#.

Can you someone here help me in printing output?
Example: User search for "How to dot this"
Then it returns:
1. do this
2. do that
3. and do that


Thank in advance for your help!
Thanh


Q and A File:
-------------------------
Q1 How to do this
1. do this
2. do that
3. and do that

End_Q1

Q2 What is bah bah bah
- This is
- It is
- And so on

End_Q2
-----------------------

Code:
----------------------
my $FAQ = "FAQ.log";
my $query = $ARGV[0];

unless (open(DBFILE, "$FAQ")) {
print "ERROR: Cannot open the System Test FAQ
exit 1;
}
my @DB = <DBFILE>;
close (DBFILE);

foreach my $line (@DB) {
if ($line =~ /\AQ_/ && $line =~ /$query/) {
print $line;
# Now need to print the following lines up to
# the End_Q#
# If query matches multiple Q#s, print all
# Question and Answers
}
}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives