Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Question in a text file and print Answers

Status
Not open for further replies.

tqlam

Programmer
Jan 10, 2008
3
CA
I have a text file with Questions and Answers in below.
I wrote something in perl to search a question(s), if match Q's, then print Answers up to End_Q#.

Can 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


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
-----------------------

Thank in advance for your help!
Thanh
 
Can you show us you code?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
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;
# I need to print the following lines up to
# the End_Q#
# If query matches multiple Q#s, print both
# Question and Answers
}
}
 
Is this school/class/course work?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It's for work, but I'm new to the perl world.
 
Code:
unless (open(DBFILE, "$FAQ")) {
   print "ERROR: Cannot open the System Test FAQ
   exit 1;
}
to
Code:
open(DBFILE, "<$FAQ") or die "ERROR: Cannot open the System Test FAQ: $!\n";

I guess you could to a print then exit 1 if you really wanted to :)
besides that I will let one of the other more knowledgeable guys answer the regex part :D

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
You have this question posted on 3 or 4 forums, I prefer to not help people that cross-post, that is my own personal policy, not the policy of Tek-Tips.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top