lisaw has asked for the wisdom of the Perl Monks concerning the following question:

I'm playing around with a script that searches a flat txt db. I'm trying to figure out how to adjust it so that the search isn't case sensitive. Anyone have any suggestions? BTW: Happy Thanksgiving!
use CGI; $datafile = "datafile.dat"; my $q = new CGI; $url = $q->url(-base); $refer = $ENV{HTTP_REFERER}; read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'}); $buffer =~ tr/+/ /; $buffer =~ s/\r/ /g; $buffer =~ s/\n/ /g; @pairs = split(/&/,$buffer); foreach $pair(@pairs){ ($key,$value)=split(/=/,$pair); $formdata{$key}.="$value"; } $search=$q->param('category'); open(INFO, "$datafile"); # Open db for reading @array=<INFO>; close (INFO); foreach $line (@array){ if ($line =~ /$search/){ ($company,$email,$member)=split(/\|/,$line); @array = sort(@array); print <<End_of_line; $company End_of_line

Replies are listed 'Best First'.
Re: Search DB case-sensitive
by Abigail-II (Bishop) on Nov 28, 2002 at 19:02 UTC
    Use the /i regex modifier.

    Abigail

      Hi Abigail, I'm still relatively new to this...where would I put the /i regex? Thanks

        /i goes at the end of the regex, eg. $text =~ s/foo/bar/i;

        I'd recommend you look at perlop for a description of how to use the various regexp operators in Perl, as well as perlre for an in-depth discussion on how to use regexps.


        _______________
        DamnDirtyApe
        Those who know that they are profound strive for clarity. Those who
        would like to seem profound to the crowd strive for obscurity.
                    --Friedrich Nietzsche
Re: Search DB case-sensitive
by dws (Chancellor) on Nov 28, 2002 at 19:41 UTC
    The form processing in this script is seriously broken. It'll work as long as the person at the browser is kind enough to type alphanumerics, but will break for many other characters. The risk you run is that someone (maybe you) will copy code out of this script later, and will then have to debug it when it doesn't work. use CGI; CGI.pm does form processing the right way. Use it.

Re: Search DB case-sensitive
by rasta (Hermit) on Nov 28, 2002 at 19:08 UTC
    Just change
    if ($line =~ /$search/){
    to
    if ($line =~ /\Q$search\E/i){
    i modifier makes search case-insensitive and \Q and \E prevents fails on some symbols in the category parameter.

    -- Yuriy Syrota
      Thank you!!! That did the trick! Happy Thanksgiving, Lis
Re: Search DB case-sensitive
by rdfield (Priest) on Nov 29, 2002 at 10:45 UTC
    When Monks write use CGI or die; they don't mean 'just add the line use CGI; to your existing code and everything will be OK' they mean 'use the functionality provided by the CGI module instead of trying to debug the insecure and generally wrong parameter parsing code you wrote'.

    rdfield