
rGeoffrey
User
/ Moderator
Nov 28, 2000, 8:45 AM
Post #2 of 3
(264 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.
|