in reply to using foreach with a regex

i want to know how i can use "foreach" Loop in this situation instead of the "for" loop
for and foreach are exact synonyms, so whenever you can one, you can also use the other instead.

But maybe it's easier to use while ($string =~ /regex/g) { ... } instead of the combination of for and </c> (unless you want to limit the number of possible matches).

Replies are listed 'Best First'.
Re^2: using foreach with a regex
by ikegami (Patriarch) on Dec 05, 2008 at 15:50 UTC

    whenever you can one, you can also use the other instead.

    Almost.

    >perl -le"sub for { print 1 } sub foreach { print 2 } &for;" 1 >perl -le"sub for { print 1 } sub foreach { print 2 } &foreach;" 2

    and

    >perl -le"print 'for';" for >perl -le"print 'foreach';" foreach

    This post is not to be taken seriously :)

      whenever you can one, you can also use the other instead.
      Almost.
      Almost. moritz just said that you can use the other, not that you'll get the same result. :-)
        Well, you cannot (unless you want to include fatal (run time) errors in 'not getting the same result').
        $ perl -wE 'sub for {say "Hello"} &for' Hello $ perl -wE 'sub foreach {say "Hello"} &for' Undefined subroutine &main::for called at -e line 1.