in reply to A way to avoid repeated conditional loops

Use m?alpha? instead of m/alpha/ to match only once:

#!/usr/bin/perl use warnings; use strict; while (<DATA>) { if (m?alpha?) { print "+ $_"; } else { print "- $_"; } } __DATA__ beta beta alpha beta beta alpha alpha beta

Results in:

- beta - beta + alpha - beta - beta - alpha - alpha - beta

Replies are listed 'Best First'.
Re^2: A way to avoid repeated conditional loops
by Deus Ex (Scribe) on Aug 25, 2011 at 13:13 UTC

    It would be a nice trick, but the match is just ignored after the first time, so the conditions keeps getting evaluated.

    Thanks though

      =~ s/^alpha/^#alpha/ ... and now do what you want with the whole file