Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^2: Pattern matching regex problem

by Anonymous Monk
on Nov 23, 2021 at 09:05 UTC ( #11139048=note: print w/replies, xml ) Need Help??

in reply to Re: Pattern matching regex problem
in thread Pattern matching regex problem

Ok, I see the difference. So, how would I tell it to print 'case 1' if 'M' is present, and 'case 2' if it's not?

Replies are listed 'Best First'.
Re^3: Pattern matching regex problem
by hippo (Bishop) on Nov 23, 2021 at 10:01 UTC

    If all you want to do is check the presence of M using tr/// then it is simple:

    #!/usr/bin/env perl use strict; use warnings; my $s='IIIIIIIIIIIIIIIIIIIIIIIIIIOOOOOSSSSS'; print "$s: " . ($s =~ tr/M/M/ ? "case1\n" : "case2\n"); $s .= 'M'; print "$s: " . ($s =~ tr/M/M/ ? "case1\n" : "case2\n");


      Or, if you don't care about tr///:

      if ($s =~ /M/) { print "case1\n"; } else { print "case2\n"; }

        tr should be faster, though.

      tr/M// is sufficient.

        Indeed it is ... as any golfer would know. :) Speaking of which, note that y is an oddball synonym for tr -- added, not for golfers (because code golf didn't exist back then), but to entice diehard sed devotees to the new frontier of Perl. I have to give a further honourable mention to y///c -- aka Abigail's length horror -- because it's one character shorter than the prosaic length.

        Finally, note that Perl's flexibility of being able to choose your own delimiter has made tr a favourite of obfuscators (and bored golfers) ... as I remember from this amusing expression:

        employed in a (non-winning) entry in the pioneering Get Even golf game of 2002.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139048]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2022-01-17 17:16 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.