in reply to Re: Can I get some help with a regex please
in thread Can I get some help with a regex please

The question isn't so much about the "reason" I need to do this- more like a better WAY to do it.

After 30 years of programming Perl, and hours with Larry Wall and Damian and other gurus, my mindset is always CAN I DO THIS BETTER IN A REGEX?

Reluctantly , I sometimes have to capitulate and conclude NO, there is no "better regex approach". This MIGHT be that. The tite loop I showed was the shortest and clearest solution so far.

The real question is,

are there RHS "tricks" I can do with $1 $2 ... ? like a way to repeat $1 $n times? And a way to know what iteration each is?
It would seem "perlish" to have this capacity. Like:
s/(block)/$1{$n}/
where $1 is repeated $n times.. Im not sure about a way to know which iteration its on- maybe a reserved PerlVar?

Replies are listed 'Best First'.
Re^3: Can I get some help with a regex please
by cavac (Prior) on Jun 14, 2023 at 21:52 UTC

    In any sort of production code, i would go so far as to recommend a clearer if slower classic C-style for loop. Yes, it's not optimized, but it is maintainable by someone who isn't a perl expert:

    my $result = ''; for(my $i = 0; $i < $n; $i++) { $result .= "$i cat\n\man\n"; }

    Or a slightly different variant that would lead itself to easier debugging:

    my @lines; for(my $i = 0; $i < $n; $i++) { push @lines, "$i cat"; push @lines, "man"; } #print Dumper(\@lines); my $result = join("\n", @lines);

    Unless it's really timing critical code (in which case thinking about doing the stuff in XS/Inline::C is also an option), long term maintainability and readability is (almost) always more important than saving a couple percent CPU cycles.

    So the answer, at least from my point of view: Can you do it faster with a regular expression: Maybe. Can you do it better? Not a chance, unless you add a few long paragraphs of explanations in the code comments that clearly lays out how to chance/update the code in a few years to fit the changing requirements of the project.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP