Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] How do you ignore case

Expand Messages
  • Sam Munzani
    ... Regex would be better solution if you want to catch Jeff or jEFF too. Sam [Non-text portions of this message have been removed]
    Message 1 of 7 , Nov 1, 2002
      Jeff 'japhy' Pinyan wrote:

      >On Sep 2, calcfreak2003 said:
      >
      >
      >
      >>I need some code to check whether a scalar is (or contains) a certain word
      >>and I can't seem to figure out how to get it to ignore case
      >>
      >>
      >
      >If you want to see if it *is* a certain word, case-insensitive, do:
      >
      > if (lc($word) eq "jeff") { ... }
      >
      >or
      >
      > if (uc($word) eq "JEFF") { ... }
      >
      >If you want to see if it CONTAINS the word, do the same thing, but with
      >index():
      >
      > if (index(lc $string, "jeff") > -1) { ... }
      >
      >or
      >
      > if (index(uc $string, "JEFF") > -1) { ... }
      >
      >There's no reason to drag a regex into things.
      >
      >
      >
      Regex would be better solution if you want to catch "Jeff" or "jEFF" too.

      Sam



      [Non-text portions of this message have been removed]
    • daymobrew@yahoo.com
      ... certain word ... Since no one else has posted Benchmark results... ========= CODE: #!/usr/bin/perl -w use Benchmark qw( cmpthese ); my $string = This long
      Message 2 of 7 , Sep 3, 2004
        --- In perl-beginner@yahoogroups.com, Jeff 'japhy' Pinyan <japhy@p...>
        wrote:
        > On Sep 2, calcfreak2003 said:
        >
        > >I need some code to check whether a scalar is (or contains) a
        certain word
        > >and I can't seem to figure out how to get it to ignore case
        >
        > If you want to see if it *is* a certain word, case-insensitive, do:
        >
        > if (lc($word) eq "jeff") { ... }
        >
        > or
        >
        > if (uc($word) eq "JEFF") { ... }
        >
        > If you want to see if it CONTAINS the word, do the same thing, but with
        > index():
        >
        > if (index(lc $string, "jeff") > -1) { ... }
        >
        > or
        >
        > if (index(uc $string, "JEFF") > -1) { ... }
        >
        > There's no reason to drag a regex into things.
        >
        > --
        > Jeff "japhy" Pinyan % How can we ever be the sold short or
        > RPI Acacia Brother #734 % the cheated, we who for every service
        > http://japhy.perlmonk.org/ % have long ago been overpaid?
        > http://www.perlmonks.org/ % -- Meister Eckhart

        Since no one else has posted Benchmark results...

        =========
        CODE:
        #!/usr/bin/perl -w

        use Benchmark qw( cmpthese );

        my $string = "This long string contains the word BenChmark that I am
        seeking";
        my $var = 0;

        cmpthese 10_000_000, {
        'RegEx' => sub { if ( $string =~ /BENCHMARK/i ) {$var++} },
        'uc' => sub { if ( index( uc( $string ), "BENCHMARK" ) > -1 )
        {$var++} },
        };
        =========
        RESULTS:
        Benchmark: timing 10000000 iterations of RegEx, uc...
        RegEx: 10 wallclock secs ( 8.57 usr + 0.00 sys = 8.57 CPU) @
        1166861.14/s (n=10000000)
        uc: 13 wallclock secs (11.60 usr + 0.00 sys = 11.60 CPU) @
        862068.97/s (n=10000000)
        Rate uc RegEx
        uc 862069/s -- -26%
        RegEx 1166861/s 35% --
        =========

        So, the regex code here is 35% faster.
      • Dave Gray
        ... You re misunderstanding Jeff s solution. He normalizes the input by converting Jeff or jEFF to all upper- or lower-case before comparing it to jeff
        Message 3 of 7 , Sep 3, 2004
          > >If you want to see if it *is* a certain word, case-insensitive, do:
          > >
          > > if (lc($word) eq "jeff") { ... }
          > >or
          > > if (uc($word) eq "JEFF") { ... }
          > >
          > >If you want to see if it CONTAINS the word, do the same thing, but with
          > >index():
          > >
          > > if (index(lc $string, "jeff") > -1) { ... }
          > >or
          > > if (index(uc $string, "JEFF") > -1) { ... }
          > >
          > Regex would be better solution if you want to catch "Jeff" or "jEFF" too.

          You're misunderstanding Jeff's solution. He normalizes the input by
          converting "Jeff" or "jEFF" to all upper- or lower-case before
          comparing it to "jeff" or "JEFF" which is a perfectly good solution,
          and a matter of personal taste (vs. regex) as long as speed is not a
          critical factor.
        Your message has been successfully submitted and would be delivered to recipients shortly.