Newbie resquests help w/ small if statement problem...

Hello,
Today I wrote my very first search script using PERL!
It simply opens my current email list and if the address supplied matches the one on file it displays it for me.

Currently the conditions below are fine:
1. prints out the "match found". (yes)
2. when you fail to enter in a address it returns "no email address given!" (yes)
3. It requires you to have so many characters as cheap fail safe for
me to catch a sort of incomplete address. (yes)

-->My only problem is that I for some reason no matter how I arrange things, I just can't seem to get it to print to screen my last condition which would equal for me displaying "NO MATCH FOUND!" (no)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#unless ($Match eq 1) { print "Email was not found!"; }
$Match = 1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I thought the lines above would be ok, but it is not working properly when the comment is removed in the real script? it then displays both the "match found" and "match not found" together?
Any help from you would be greatly appreciated...

I am so close :)

Thank you in advance for your valued time~

=====================================================================
HERE IS MY SEARCH SCRIPT BELOW
=====================================================================
#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);

$email_file = "/cgi-bin/mailinglist/mailinglist.txt";
$footer"";

&Parse;

sub Parse {
local($name, $value, $buffer, $pair, $hold, @pairs);

if($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ s/
//g;
$name =~ s/
//g;
$value =~ s/
//g;
$value =~ s/
//g;

#This section checks the value portion (user input) of
#all name/value pairs.
$value =~ s/(<[^>]*>)//g; #Remove this tag to permit HTML tags
$value =~ s/|//g;
$value =~ s///g;
$value =~ s/s-w.+//g;
$value =~ s///g;
$value =~ s/|//g;
$value =~ s/\//g;
$value =~ s/system(.+//g;
$value =~ s/grep//g;
$value =~ s/srms//g;
$value =~ s/srfs//g;
$value =~ s/..([/:]|$)//g;
$value =~ s/< *((SCRIPT)|(APPLET)|(EMBED))[^>]+>//ig;

#This section checks the value portion (from element name) of
#all name/value pairs. This was included to prevent any nasty
#surprises from those who would hijack you forms!
$name =~ s/(<[^>]*>)//g;
$name =~ s///g;
$name =~ s/s-w.+//g;
$name =~ s///g;
$name =~ s/|//g;
$name =~ s/\//g;
$name =~ s/system(.+//g;
$name =~ s/grep//g;
$name =~ s/srms//g;
$name =~ s/srfs//g;
$name =~ s/..([/:]|$)//g;
$name =~ s/< *((SCRIPT)|(APPLET)|(EMBED))[^>]+>//ig;

$FORM{$name} = $value;
}
}

1;

print "Content-type: text/html

";

print ";

######################################
# Display Search Form #
######################################

print
Enter email address:





EndOfSearch

######################################
# End Search Form #
######################################


######################################
# Modify search string #
######################################
$search = lc($FORM{'address'}) ;
######################################
# End Modify search string #
######################################

######################################
# Display Search Results #
######################################

print
Display Search Results


Search Results:



End_of_Top

open(SUBSCRIBERS,"$email_file");
@addresses=;
close(SUBSCRIBERS);

if ($search ne "") {

#unless ($Match eq 1) { print "Email was not found!"; }

foreach $_(@addresses) {
chomp($_);


if(length($search) < 2) {
print "Search must contain full email address!";
print "
$footer";
exit;
}

if($_ =~ m/$search/gi) {
print

Match found! - $_


End_of_File
$Match = 1;
}
}
} else {
print "No email address given";
}

print



End_of_Bottom

print "
$footer";
exit;

Comments

  • [b][red]This message was edited by Jonathan at 2003-7-23 13:45:52[/red][/b][hr]
    Hi,

    : Today I wrote my very first search script using PERL!
    Yay! Welcome to Perl. :-)

    : It simply opens my current email list and if the address supplied
    : matches the one on file it displays it for me.
    :
    : Currently the conditions below are fine:
    : 1. prints out the "match found". (yes)
    : 2. when you fail to enter in a address it returns "no email address
    : given!" (yes)
    : 3. It requires you to have so many characters as cheap fail safe for
    : me to catch a sort of incomplete address. (yes)
    As a possibly better way, try matching against this pattern:-
    /^[w_.]+@[w_.]+.[w]{2,4}$/

    : -->My only problem is that I for some reason no matter how I arrange
    : things, I just can't seem to get it to print to screen my last
    : condition which would equal for me displaying "NO MATCH FOUND!" (no)
    : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    : #unless ($Match eq 1) { print "Email was not
    : found!"; }
    : $Match = 1;
    For numerical comparations, use == rather than eq.

    [code]unless ($Match == 1) {
    print 'Email was not found!';
    }[/code]

    : I thought the lines above would be ok, but it is not working
    : properly when the comment is removed in the real script? it then
    : displays both the "match found" and "match not found" together?
    : Any help from you would be greatly appreciated...
    I think you're putting it in the wrong place.


    : if ($search ne "") {
    :
    : [red] #unless ($Match eq 1) { print "Email was not found!"; }[/red]
    :
    : foreach $_(@addresses) {
    : chomp($_);
    :
    :
    : if(length($search) < 2) {
    : print "Search must contain full email address!";
    : print "$footer";
    : exit;
    : }
    :
    : if($_ =~ m/$search/gi) {
    : print <<End_of_File;
    : <tr>
    :
    : Match found! - $_
    :
    :
    : End_of_File
    : $Match = 1;
    : }
    : }
    : } else {
    : print "No email address given";
    : }
    : [red]Shouldn't it be here? I think it should...[/red]
    : print <<End_of_Bottom;
    : </center>
    :
    :
    :
    : End_of_Bottom


    Also if you're using -w I think you may want to be predeclaring your variables, using the "my" keyword before their first use (but be aware that my also limits them to the current scope, e.g. they are destroyed at the next }, so you have to write my $variable; outside of the structure) or putting them in a specific package (e.g. $main::Match or $::Match rather than $Match). I prefer using my as you don't end up with package names all over.

    One other style note (trying to be helpful, not critical) is that you don't need to explicitly write $_ - that's the beauty of it.

    : foreach $_(@addresses) {
    : chomp($_);
    Can be written as:-
    [code]foreach (@addresses) {
    chomp;
    #etc.
    [/code]

    As $_ is the "default" variable.

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion