misterperl has asked for the wisdom of the Perl Monks concerning the following question:
Briefly I need to substitute the ordinal match sequence number for each match, like:
I need a perlvar that has the nth match so I can write something like s/(cat)/^1/smg where ^1 would be the number of the nth "cat": matched. In that example I'd get:cat dog cat mouse eel cat housecat catamaran fish
OF COURSE I can do this in a loop. Or with a /e.. I want a substitution with a Perlvar of the nth match, if one exists? TYVM!1 dog 2 mouse eel 3 house4 5amaran fish
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl substitute with the nth match
by choroba (Cardinal) on Jan 11, 2023 at 19:04 UTC | |
by LanX (Saint) on Jan 13, 2023 at 13:50 UTC | |
|
Re: Perl substitute with the nth match
by ikegami (Patriarch) on Jan 11, 2023 at 19:02 UTC | |
|
Re: Perl substitute with the nth match
by haukex (Archbishop) on Jan 11, 2023 at 19:08 UTC | |
|
Re: Perl substitute with the nth match
by johngg (Canon) on Jan 11, 2023 at 22:21 UTC | |
by haukex (Archbishop) on Jan 12, 2023 at 18:47 UTC | |
|
Re: Perl substitute with the nth match
by misterperl (Friar) on Jan 11, 2023 at 20:01 UTC | |
by hippo (Archbishop) on Jan 12, 2023 at 09:28 UTC |