Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: search for exact word only
by Roy Johnson (Monsignor) on Sep 27, 2004 at 14:15 UTC | |
by Anonymous Monk on Sep 27, 2004 at 16:54 UTC | |
by Roy Johnson (Monsignor) on Sep 27, 2004 at 17:01 UTC | |
by Anonymous Monk on Sep 27, 2004 at 18:08 UTC | |
by Roy Johnson (Monsignor) on Sep 27, 2004 at 21:25 UTC | |
| |
by muntfish (Chaplain) on Sep 28, 2004 at 08:20 UTC | |
by Anonymous Monk on Sep 28, 2004 at 10:53 UTC | |
by muntfish (Chaplain) on Sep 28, 2004 at 11:26 UTC | |
| |
|
Re: search for exact word only
by muntfish (Chaplain) on Sep 27, 2004 at 14:19 UTC | |
|
Re: search for exact word only
by Grygonos (Chaplain) on Sep 27, 2004 at 15:36 UTC | |
by muntfish (Chaplain) on Sep 28, 2004 at 08:17 UTC | |
|
Re: search for exact word only
by TedPride (Priest) on Sep 27, 2004 at 21:54 UTC |