in reply to Match upper and lower case

IMHO TMTOWTDI
if (uc $error eq "ERROR"){ # . . . }
or
if (lc $error eq "error"){ # . . . }
Although, I would prefer Zaxo's first example because it seems more efficient (if for no other reason, but conservation of code :-)

-Jim

Replies are listed 'Best First'.
Re^2: Match upper and lower case
by blazar (Canon) on Jan 30, 2005 at 15:04 UTC
    IMHO TMTOWTDI
    if (uc $error eq "ERROR"){ # . . . }
    or
    if (lc $error eq "error"){ # . . . }
    Although, I would prefer Zaxo's first example because it seems more efficient (if for no other reason, but conservation of code :-)
    Note that this is not strictly equivalent to Zaxo's code since /error/i checks (case insensitively) for the presence of the string 'error' and not for equality to it (up to case insentiveness).

    More importantly: "more efficient"? Well, I premise that I'd probably use /error/i or /^error$/i (this really depends on what can there be in $_) myself in most reasonable cases, but running

    #!/usr/bin/perl use strict; use warnings; use Benchmark qw/:all :hireswallclock/; my $test_str='error'; my @test=map { my @chr=("\0", " "); local $_=$test_str; s/./$& ^ $chr[rand 2]/ge} 1..100; no warnings 'void'; cmpthese -30, { regex => sub { /^\Q$test_str\E$/i for @test }, lc => sub { $test_str eq lc for @test} }, 'all'; __END__
    I get
    Benchmark: running lc, regex for at least 30 CPU seconds... lc: 34.068 wallclock secs (34.10 usr 0.00 sys + 0.00 cusr 0 +.00 csys = 34.10 CPU) @ 19917.24/s (n=679178) regex: 31.518 wallclock secs (31.53 usr 0.00 sys + 0.00 cusr 0 +.00 csys = 31.53 CPU) @ 13689.50/s (n=431630) Rate regex lc regex 13690/s -- -31% lc 19917/s 45% --
    and
    Benchmark: running lc, regex for at least 30 CPU seconds... lc: 31.4462 wallclock secs (31.45 usr 0.00 sys + 0.00 cusr +0.00 csys = 31.45 CPU) @ 26138.57/s (n=822058) regex: 31.8579 wallclock secs (31.86 usr 0.00 sys + 0.00 cusr +0.00 csys = 31.86 CPU) @ 14326.49/s (n=456442) Rate regex lc regex 14326/s -- -45% lc 26139/s 82% --
    under Windows 98 and Linux (kernel 2.6.10) respectively. Perl 5.8.6 in both cases...