http://qs1969.pair.com?node_id=11139050


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

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");

🦛

Replies are listed 'Best First'.
Re^4: Pattern matching regex problem
by bibliophile (Prior) on Nov 23, 2021 at 15:00 UTC

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

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

      tr should be faster, though.

Re^4: Pattern matching regex problem
by ikegami (Patriarch) on Nov 23, 2021 at 21:00 UTC

    tr/M// is sufficient.

      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:

      ($.|y|||c|y|a|||y|e|||y|i|||y|o|||y|u|||y|y||)||print
      employed in a (non-winning) entry in the pioneering Get Even golf game of 2002.