in reply to Re: Numbering instances of a String in a file
in thread Numbering instances of a String in a file
Needless sprintf, needless match, and you should probably separate the number from the stars with a space. I'd also require at least 3 stars to trigger a match.
my $cnt = '0000'; while (<>) { s/^(\*{3,})$/"$1 ". ++$cnt/e; print; }
Optimised: (requires 5.10+)
my $cnt = '0000'; while (<>) { s/^\*{3,}\K$/" ". ++$cnt/e; print; }
As a one-liner: (Second version requires 5.10+)
perl -pe'BEGIN { $cnt = '0000' } s/^(\*{3,})$/"$1 ". ++$cnt/e' perl -pe'BEGIN { $cnt = '0000' } s/^\*{3,}\K$/" ". ++$cnt/e'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Numbering instances of a String in a file
by JavaFan (Canon) on Jun 07, 2010 at 23:21 UTC | |
by ikegami (Patriarch) on Jun 08, 2010 at 03:37 UTC |