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

my @bintext2 = grep {$_ =~ "$input"} @bintext;
Where $input is the <STDIN>, how can I make this grep search case insensitive?
The rest of the code is here:
#!usr/bin/perl -w # nl.plx use strict; open FILE,"<$ARGV[0]"; my @foo = <FILE>; my $lineno = 1; print "Enter in search keyword: "; my $input = <STDIN>; chomp($input); my @bintext = qx(strings.exe -n 4 Page.txt); print "@bintext\n"; my @bintext2 = grep {$_ =~ "$input"} @bintext; print "@bintext2\n";
Thank You!!!

Replies are listed 'Best First'.
Re: GREP case insensitivity
by ccn (Vicar) on Nov 13, 2008 at 20:36 UTC
Re: GREP case insensitivity
by Corion (Patriarch) on Nov 13, 2008 at 20:37 UTC

    See perlre. Also, you likely want to use real regular expressions in your match syntax and not strings:

    my @bintext2 = grep { $_ =~ /$input/i } @bintext; # or even shorter my @bintext2 = grep { /$input/i } @bintext;
Re: GREP case insensitivity
by duelafn (Parson) on Nov 13, 2008 at 23:59 UTC

    There is a good chance that you would want to escape $input so that special regex characters match exactly, rather than taking on their special meaning.

    my @bintext2 = grep { /\Q$input\E/i } @bintext;

    This way things like "*", "+", and "-" match literally.

    Good Day,
        Dean

      Then again, there's an equally good chance that the user wants the special regex characters to be applied as such, and not as literals. In that case, users can supply escapes and "\Q" (or not) according to their preference.
Re: GREP case insensitivity
by graff (Chancellor) on Nov 14, 2008 at 00:04 UTC
    If you are using the grep function simply to grab the items in a list that match a particular pattern, you don't even need the curlies:
    my @bintext2 = grep /$input/i, @bintext;
    (note the comma after the regex)

    You only need curlies if you are using grep with a code snippet (item is selected if the code returns true) -- e.g.:

    my @bintext2 = grep { /$input/i and length()>20 } @bintext;
    (note: no comma after the code block)
      You only need curlies if you are using grep with a code snippet

      The reason your example needs curlies is because the  and operator has lower precedence than the comma operator.   If you use the  && operator then a comma works.

      my @bintext2 = grep /$input/i && length > 20, @bintext;
Re: GREP case insensitivity
by Hue-Bond (Priest) on Nov 14, 2008 at 08:21 UTC

    You can also do grep { lc $_ eq lc $input }

    --
    David Serrano

      Worked like a charm! Thanks for all the help!

      Tim