Hi all :)
I am trying to write a script to search through a data file. This is how data is stored in the file.
<ref>
<provnc>
<aulist>
<author> Bin Laden
</aulist>
<year>1990
<source> Cambridge University Press, Cambridge UK, 1st edition
<id>1
<keywords>
<key>terrorism
<key>whatever
</keywords>
</provnc>
<title> Terrorism
</ref>
<ref>
<provnc>
<aulist>
<author> Sydney
</aulist>
<year>1990
<source> Cambridge University Press, Cambridge UK, 1st edition
<id>1
<keywords>
<key>nothing
<key>whatever
</keywords>
</provnc>
<title> Terrorism
</ref>
This is my current search script.
#!/usr/bin/perl
##use CGI ':standard';
## Store the data here
my %data;
@keywords = ();
##Get the string to search
$search=param('search');
print "Content-type:text/html\n\n"; #Content Header
print <<End_of_head;
<html>
<head><title>Display Results</title></head>
<body>
End_of_head
open (FH, "<data.sgl") or die("Unable to open data.sgl\n");
while(<FH> ) {
if (/\<author\>\s*(\D*)$search/i )
{
get_data();
print_data();
cleararray();
}
}
print <<End_of_Doc;
</body>
</html>
End_of_Doc
sub get_data {
while(<FH> ) {
$data{author} = $1 if /\<author\>\s*(\D*)/i;
$data{year} = $1 if /\<year\>\s*(\d{4})/i;
$data{source} = $1 if /\<source\>\s*(\D*)/i;
$data{id} = $1 if /\<id\>\s*(\d+)/i;
push(@keywords, $1) if /\<key\>\s*(\w+)/i; ## store keys in an arr
+ay
$data{title} = $1 if /\<title\>\s*(\D*)/i;
return if /\<\/ref\>/;
}
}
sub print_data{
print <<End_of_print;
Author:$data{author} = $1
Year: $data{year}
Source: $data{source}
Keys: @keywords
Title: $data{title}
End_of_print
}
sub cleararray{
@keywords = ();
}
close(FH);
But the problem is after it finds the author, it prints out the rest of the record but not the author. The author is not displayed. I am thinking of taking the whole record into an array after the <ref> tag up to the </ref> and then searching through the record in the array and if search term is found, send the array to print data. After that clearing the data in the array and loading the next record to search again. But I cant figure out how to do it.
Please explain if there is a better way.
Thanks :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.