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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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



Nov 27, 2000, 4:11 PM

Post #1 of 3 (418 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 would find XXX@com.Com as true. here is my code: Anu thoughts on getting this to work?

close DATA;

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

User / Moderator

Nov 28, 2000, 8:45 AM

Post #2 of 3 (418 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);

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.


Nov 29, 2000, 5:22 AM

Post #3 of 3 (418 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