Hello,
I have I simple Perl search script which search a database and display the results.
Database is flat text file, pipe (|) delimited. From a search box on a web page, the information is sent to the perl search search script. This then displays the results from the database in the format of the template.
It works, but has one lack: it't insufficiently exact.
Example: I search for a word 'ABBA', alongside with the given word it displays also other words containing a word ABBA inside:
'Barabba'
'Black Sabbath'
'Cabballero'

That is is I just want improve a search result filtration that the script showed only an exact word only.
I am new in Perl, could someone improve this feature impeccably, please? Just correct my code (red line corrected) Thank you.

#!/usr/bin/perl ########################################################## ########################################################## #%FORM = parse_cgi(); print "Content-type: text/html\n\n"; (my $head, my $tmp, my $foot) = get_html($HTML_template); $qs=$ENV{'QUERY_STRING'}; ##read db my @data = read_file($CSV_file); chomp $data[0]; my @fields= split('\|', shift @data); $base_length = @data; error("You have bad file!") if !@fields; error("Database is clear!") if $base_length<1; if($qs =~m/header=([^\&\Z]*)/){push @header,$1;} if($ID_use && $qs =~m/show=([^\&\Z]*)/){ @data = search($1,$ID_field_name); } else{ @conditions=split(/&/,$qs); my $a=0; foreach (@conditions){ ($name, $value) = split(/=/, $_); if($name eq 'search'){ $FORM{search} = $value; @data = search($value, $header[0]); } elsif($name eq 'header'){} elsif($_=~/([^=<>!]+)!=([^=<>!]+)/){@data = search($value, $1, + "!=");} elsif($_=~/([^=<>!]+)=([^=<>!]+)/){@data = search($value, $1); +} $a++; } } my $result; ##matched data foreach(@data){ chomp; @line = split('\|', $_); $a=0; %INSERT=(); foreach(@fields){$INSERT{$_} = $line[$a++];} $result.=get_record($tmp) } %INSERT=(); $INSERT{'#_matches'} = @data; $INSERT{'#_total'} = $base_length; $result = $no_matches_found."<br>" unless @data; print get_record($head), $result, get_record($foot); undef $result; undef $head; undef $foot; exit; ######################################################### sub search{ my $word=shift; my $field=shift; my $action=shift; $word=~tr/+/ /; $word=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg; my $position=-1; my $a=0; if($field){ $field=~tr/+/ /; $field=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg; foreach(@fields){$position=$a if $_ eq $field; $a++;} } my %match; $word =~s/ +/ /g; my @new_data=(); my @keys= split(" ", $word); if($action eq '!='){for(0..@data-1){$match{$_} = 1;}} foreach $key (@keys){ $a=0; foreach $record (@data){ @line = split('\|', $record); if($field && $position>-1){ if($action eq '!='){ $match{$a} = 0 if $line[$position]=~m/\Q$key/i; } else{$match{$a} = 1 if $line[$position]=~m/\Q$key/i;} } else{ foreach(@line){if ($_=~m/\Q$key/i){$match{$a} = 1; las +t;}} } $a++; } } $a=0; my $b=0; foreach(@data){ $new_data[$b++] = $_ if $match{$a}; $a++; } return @new_data; } sub get_record{ my $text = $_[0]; $text =~ s{<<(.*?)>>}{exists($INSERT{$1}) ? $INSERT{$1} : ""}gsex; return $text; } sub get_html{ my @txt = read_file($_[0]); my $txt; foreach(@txt){$txt.=$_;} $txt=~/(.*)<template>(.*)<\/template>(.*)/s; error("Template-tag not found!") if !$1 or !$2; return ($1,$2,$3); } sub read_file{ open(F, $_[0]) || error("Can't open file $_[0]!"); my @data = <F>; close F; return @data; } sub error{ print "<html><head><title>Error</title>$style</head><body><br><br> +<br><font color=red><h3>$_[0]</h3></font></body></html>"; exit; } ##########################################################

janitored by ybiC: Balanced <readmore> tags around longish codeblock, as per Monastery convention

20040928 Edit by castaway: Changed title from 'tenerif'


In reply to search for exact word only by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.