in reply to Increase number per regex replacement

Just for fun, here's an alternative version without /e ;-)
$i = 0; @no = qw(one two three four five); foreach ( @no ){ s/e/foo.$i/ && $i++ while /e/; } print "@no\n"; __END__ onfoo.0 two thrfoo.1foo.2 four fivfoo.3

--
perl -pe "s/\b;([st])/'\1/mg"

Replies are listed 'Best First'.
Re: Re: Increase number per regex replacement
by blakem (Monsignor) on Jan 08, 2002 at 10:46 UTC
    Ah, but what if you needed to use "fee.$i" as the replacement string instead of "foo.$i"?

    Fee fi foo fum, I smell the infinite loop of an eratum...

    -Blake

      True true, but this was meant to be a whimscial/goldbergian solution. You'd have to be wary of the same issue with /e though not /ge ;-) Of course this fixes that:
      $i = 0; @no = qw(one two three four five); foreach ( @no ){ my $str = ''; while( s/([^e]*)e// ){ $str .= $1 . "fee" . $i++; } $_ = $str . $_; #UPDATED: Append addresses Re: to this node } print "@no\n";

      --
      perl -pe "s/\b;([st])/'\1/mg"

        Hmmm... I don't mean to keep poking at a not-too-serious answer, but I don't think that works exactly right either. What happens with numbers like 'seven' or 'ten' that have an 'e' in the middle, but not at the end? The last non-matching-non-'e' part gets removed.

        Update: for that matter, the numbers w/o an 'e' in them at all are being nulled out...
        Update2: Updated code now seems to work properly, $str is built up while $_ is torn down...

        -Blake