Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

mixed case with match

by catfish1116 (Beadle)
on Sep 21, 2020 at 19:03 UTC ( [id://11122024]=perlquestion: print w/replies, xml ) Need Help??

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

Quick question. How do I get the script below to match on mixed case:

my $sum = 0; while ( <>) { $sum++; last if m/perl/; } print "There were $sum lines until I found perl\n";

TIA The Catfish

Replies are listed 'Best First'.
Re: mixed case with match
by kcott (Archbishop) on Sep 21, 2020 at 19:21 UTC

    G'day catfish1116,

    You could use the 'i' modifier. See "perlre: Modifiers".

    You might also want to capture the exact variation matched so your subsequent print is more accurate; for instance, "... until I found perl", "... until I found Perl", "... until I found pErL", etc. See "perlre: Capture groups".

    — Ken

Re: mixed case with match
by AnomalousMonk (Archbishop) on Sep 21, 2020 at 19:11 UTC
Re: mixed case with match
by AnomalousMonk (Archbishop) on Sep 21, 2020 at 21:23 UTC

    It might also be useful to include some boundary assertions to avoid hitting on words like "perlaceous" or "properly" (untested):
        m{ (?i) \b perl \b }xms
    (I prefer to use the inline (?i) rather than the /i modifier: I think it's more noticeable.)


    Give a man a fish:  <%-{-{-{-<

Re: mixed case with match
by BillKSmith (Monsignor) on Sep 21, 2020 at 23:06 UTC
    Not all combinations of upper and lower case are valid. 'Perl' is the name of the language. 'perl' is the name of the interpreter. 'PERL' is used in some book titles. (e.g. PERL IN A NUTSHELL). Use alternation to test only for the valid forms.
    last if m/\b(?:PERL|Perl|perl)\b/;
    Bill
Re: mixed case with match
by Anonymous Monk on Sep 21, 2020 at 19:44 UTC

    Relevant to the functionality but not to the OP's question:

    Built-in variable $. is the number of lines read from the current (i.e. last-read) file handle. So:

    while ( <> ) { last if m/perl/i; } print "There were $. lines until I found perl\n";

Re: mixed case with match
by gam3 (Curate) on Sep 22, 2020 at 08:30 UTC
    # But I'm not a golfer. use strict; use warnings; sub test { my ($m, $line) = (undef, 0); while ( <DATA> ) { $line++; last if (($m) = m/(perl)/i); } if ($m) { printf "There were %d lines until I found '%s'\n", $line, $m; } else { printf "I never found 'perl' in %d lines\n", $line; } } test test __END__ 1 2 3 This is peRl ok 1 2 3

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11122024]
Approved by kcott
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-04-25 01:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found