Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: can u suggest a regex for this ?

by jo37 (Deacon)
on Feb 03, 2023 at 14:59 UTC ( [id://11150125] : note . print w/replies, xml ) Need Help??

in reply to can u suggest a regex for this ?

If you want to do it with map:

map {/^cat\b/ .. /^\^cat\b/ ? s/^/#/r : $_} @a;



Replies are listed 'Best First'.
Re^2: can u suggest a regex for this ?
by misterperl (Pilgrim) on Feb 03, 2023 at 15:26 UTC
    THAT is very ELEGANT and Perlish TYVM!

    I get an error with the "r" modifier but it seems to work without it? I'm on Perl 5.8.8 maybe /r isn't supported?

      Without /r, map would modify the original array, but this is not available in very old versions.


      I'm on Perl 5.8.8

      There have been recent discussions here about using old Perls. If it's not too impertinent to ask, would you mind divulging which O/S (and which version) you are running which has this Perl version installed? I'm unaware of any O/S still supported which has it. TYVM.


        I thought I'd replied to this but I don't see it. Sadly we're on RH5.5 . Our company is VERY change-averse. Like NASA using Apollo components..
Re^2: can u suggest a regex for this ?
by misterperl (Pilgrim) on Feb 03, 2023 at 16:20 UTC
    I just went back and ++'ed your reply JO , this is a game-changer. This construct has myriad applications in general.

    I'm always looking for ways to shorten code- its generally more maintainable, easier to read, and more reliable. This is a huge win to modify certain BLOCKS of text, in one statement.

    Like to change all "dog"s to "canine" only in XML pet blocks I might use: map {/^<pets>/ .. /^<\/pets>/ ? s/dog/canine/gir : $_ } @a;

    TY you made my month. I never knew how to do this ; I've tried all sorts of approaches over the years like regex assertions, etc.

    ++ to you for sure ; Id do ++^^10 if I could!