
japhy
Enthusiast
Dec 21, 2000, 8:17 AM
Post #2 of 3
(44808 views)
|
Assuming your database only gets information appended to it (not changed in the middle or beginning), here is a simple solution for flat file databases:
use CGI; use strict; my $q = CGI->new; my $DB = "/path/to/file.db"; my $pattern = $q->param('pat'); my ($pos, @matches, $last_loc, $more); open FILE, $DB or die "can't read $DB: $!"; # jump ahead in file to continue searching seek FILE, $pos, 0 if $pos = $q->param('pos'); while (<FILE>) { chomp(my @fields = split /\|/); # assume a match if the description matches if ($fields[1] =~ /$pattern/o) { push(@matches, \@fields); $last_loc = tell(FILE) - length; # get position last if @matches == 11; # yes, 11 } } close FILE; # figure out if there's more $more = @matches == 11 and pop @matches; display(@matches); more_button($last_loc,$pattern) if $more; sub display { ... } sub more_button { my ($pos,$pat) = @_; print qq{See <a href="prog.cgi?pos=$pos&pat=$pat">more</a>.\n}; } Most of the code is self-explanatory. The nice trick, though, is using a file offset to jump ahead in the database file -- we change the place where we start looking. Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author
|