in reply to Re: Re: uninitialized values for arrays suddenly appear
in thread uninitialized values for arrays suddenly appear
The following is a quick and dirty reworking of your code to make it parse under strict and it also uses CGI.pm. It's not guaranteed to work, but if you can cobble it together, it's going to be much easier to maintain. In addition to the 'use CGI or die' post that was mentioned, you might want to read 'use strict' is not Perl to gain insight as to why I eliminated (most) of your global variables.
#!/usr/local/bin/perl -w # Define Variables # use strict; use CGI qw/:standard/; my $basedir = '/home/mysite/www/'; my $baseurl = 'http://www.mysite.com/'; my @files = ('*.htm','*.html','*/*.html'); my $title = "The Silver Machine"; my $title_url = 'http://mysite.com/'; my $search_url = 'http://www.mysite.com/search.html'; # Done # ###################################################################### +######## # Parse Form Search Information # if it's a single form value, map the value as a scalar, other wise, +the value is # and arrayref my %FORM = map { $_, @{[ param($_) ]} > 1 ? [ param($_) ] : param($_) +} param(); # Get Files To Search Through my $files = &get_files; # Search the files my ( $include, $titles ) = search( $files ); # Print Results of Search return_html( $include, $titles ); sub get_files { my @FILES; chdir($basedir); foreach my $file (@files) { my $ls = `ls $file`; my @ls = split(/\s+/,$ls); foreach my $temp_file (@ls) { if (-d $file) { my $filename = "$file$temp_file"; if (-T $filename) { push(@FILES,$filename); } } elsif (-T $temp_file) { push(@FILES,$temp_file); } } } return \@FILES; } sub search { my @FILES = @{$_[0]}; my ( %include, %titles ); my @terms = split(/\s+/, $FORM{'terms'}); foreach my $FILE (@FILES) { my $string; open(FILE,"$FILE") or die "Can't open $FILE for reading: $!"; { local $/; $string = <FILE>; } close(FILE); $string =~ s/\n//g; if ($FORM{'boolean'} eq 'AND') { foreach my $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if (!($string =~ /$term/i)) { $include{$FILE} = 'no'; last; } else { $include{$FILE} = 'yes'; } } elsif ($FORM{'case'} eq 'Sensitive') { if (!($string =~ /$term/)) { $include{$FILE} = 'no'; last; } else { $include{$FILE} = 'yes'; } } } } elsif ($FORM{'boolean'} eq 'OR') { foreach my $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if ($string =~ /$term/i) { $include{$FILE} = 'yes'; last; } else { $include{$FILE} = 'no'; } } elsif ($FORM{'case'} eq 'Sensitive') { if ($string =~ /$term/) { $include{$FILE} = 'yes'; last; } else { $include{$FILE} = 'no'; } } } } if ($string =~ /<title>(.*?)<\/title>/i) { $titles{$FILE} = "$1"; } else { $titles{$FILE} = "$FILE"; } } return ( \%include, \%titles ); } sub return_html { my %include = %{$_[0]}; my %titles = %{$_[1]}; open(FH,"../top.html") || die "couldn't open file $!\n"; my @html=<FH>; close(FH); print @html; print "\n <center>\n <h1>Search Results</h1>\n </center>\n"; print "Here ya go, they're in no particular order:<p><hr size=7 wid +th=75%><p>\n"; print "<ul>\n"; foreach my $key (keys %include) { if ($include{$key} eq 'yes') { print "<li class=bullets><span class=bullets><a href=\"$baseu +rl$key\">$titles{$key}</a></span></li>\n"; } } print "</ul>\n"; print "\n"; print "</ul><br><hr size=7 width=75%><P>\n"; print "<ul>\n<li class=bullets><span class=bullets><a href=\"$searc +h_url\">Back to Search Page</a></span></li>\n"; print "<li class=bullets><span class=bullets><a href=\"$title_url\" +>$title</a></span></li>\n"; print "</ul>\n"; print "<hr size=7 width=75%>\n"; print "</body>\n</html>\n"; }
Cheers,
Ovid
Vote for paco!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Ovid) Re(3): uninitialized values for arrays suddenly appear
by jerrygarciuh (Curate) on Sep 08, 2001 at 17:50 UTC | |
|
Re: (Ovid) Re(3): uninitialized values for arrays suddenly appear
by jerrygarciuh (Curate) on Sep 08, 2001 at 01:31 UTC | |
by runrig (Abbot) on Sep 08, 2001 at 01:59 UTC |