For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > Effective search of text file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Effective search of text file
Why Tea

2007-12-12, 8:00 am

I have a text file of acronyms of about 2MB, I would like to write a
cgi script to allow users to query for any acronym in the text file.
What is the best way of implementing it (i.e. taking the acronym as a
key to search for the expanded text)?

/Why Tea

Jeff Pang

2007-12-12, 8:00 am


--- Why Tea <ytlim1@gmail.com> wrote:

> I have a text file of acronyms of about 2MB, I would like to write
> a
> cgi script to allow users to query for any acronym in the text
> file.
> What is the best way of implementing it (i.e. taking the acronym as
> a
> key to search for the expanded text)?
>


the most common way is to use a key/value based text db.
you create the file like:

# test.db
afaik "as far as i know"
ppl "people"
asap "as soon as possible"
.....

then write a cgi script to do the query:

use CGI;
my $q = CGI->new;
my $key = $q->param("key");
my $value = 'unknown';
open FD,"test.db" or die $!;
while(<FD> ) {
next if (/^#/ || /^$/);
chomp;
my ($k,$v) = split;
next unless $k eq $key;
$value = $v;
last;
}
close FD;

print $q->header;
print $value;

__END__

(warn: didn't test it)

Good luck!

Best Regards,
Jeff (joy) Peng


________________________________________
________________________________________
____
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06...tDypao8Wcj9tAcJ


Dr.Ruud

2007-12-12, 8:00 am

jeff pang schreef:

> open FD,"test.db" or die $!;


Jeff, could you please start using lexical filehandles, more whitespace,
and 3-argument opens, when publishing example code on this list?

my $filename = "test.db";
open my $fd, "<", $filename
or die "'$filename': $!";

(yes, I prefer Texan quotes nowadays :)

--
Affijn, Ruud

"Gewoon is een tijger."

Jeff Pang

2007-12-12, 8:00 am


--- "Dr.Ruud" <rvtol+news@isolution.nl> wrote:

> jeff pang schreef:
>
>
> Jeff, could you please start using lexical filehandles, more
> whitespace,
> and 3-argument opens,


Ok I can but I don't like.
When I uploaded my cpan module with a lexical FH and 3 args open, it
can't be passed by testors, who use the Perl which version is below
than 5.6. So for the compatibility of different versions, using
traditional ways is not bad.

Best Regards,
Jeff (joy) Peng


________________________________________
________________________________________
____
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newse...tegory=shopping

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com