CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
case insensitive db search

 



spaz
Novice

Nov 27, 2000, 4:11 PM

Post #1 of 3 (398 views)
case insensitive db search Can't Post

I'm trying to search a form submitted email address in a database file and want to the address regardless of case. For example, a search for xxx@com.com would find XXX@com.Com as true. here is my code: Anu thoughts on getting this to work?

open(DATA,"$File");
@DATA = <DATA>;
close DATA;

foreach $Story (@DATA)
{
@Temp = split (/\|/, $Story);
@tempemail=/$Temp[1]/gi;
if ($FORM{'email2'} eq "@Tempemail")
{ &go_there; }
else { &dont_go; }
}


rGeoffrey
User / Moderator

Nov 28, 2000, 8:45 AM

Post #2 of 3 (398 views)
Re: case insensitive db search [In reply to] Can't Post

Do you really want to have &go_there and &dont_go inside the for loop?

Here is one possiblility

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my %addresses = &All_Names ($File);

if (exists $addresses{lc ($FORM{'email2'})}) {
&go_there ();
} else {
&dont_go ();
}

sub All_Names
{
my ($File) = @_;

open (DATA, $File) or die "Could not open $File, $!\n";
my @DATA = <DATA>;
close DATA;

my %addresses = map { lc ($_) => 1 } @DATA;
return (%addresses);
}
</pre><HR></BLOCKQUOTE>

Among the changes...

We now have a 'die' after the open. You should always consider the possiblity that open will fail.

The &go_there and &dont_go are now done just once after the checking.

Rather then foreach through the data looking for the match, and then continuing to loop even after it is found, we now create a hash so we can see if the key is in the hash.

Replace the regular expression with the 'i' modifier, we just lower case everything to avoid confusion.

As an added bonus the %addresses hash can be used over and over again during the lifetime of the script so you can check for other users without having to loop again because the price of building the hash has already been paid.


spaz
Novice

Nov 29, 2000, 5:22 AM

Post #3 of 3 (398 views)
Re: case insensitive db search [In reply to] Can't Post

Thanks -- I didn't think much about including the subroutines in the loop. Good call!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives