misterperl has asked for the wisdom of the Perl Monks concerning the following question:
the result I want ismouse cat 1 2 3 ^cat deer eel cat furbaby elk ^cat dog fish
This can be in an array (one element per line) or a scalar. Usually I just loop thru the list and set a flag when hit cat, and unset it after leaving ^cat, and add the # if flagged. Which is like how I would do this in C. Every time I solve a problem in my brain in C, I always think to myself surely Perl has a better approach!.. Fugly code:mouse #cat #1 #2 #3 #^cat deer eel #cat #furbaby #elk #^cat dog fish
my $comment=0; for (@a) { $comment++ if /^cat\b/; $_ = '#'.$_ if $comment; $comment-- if /^\#\^cat\b/; }
Can you suggest a brief solution with a single no-looping regex, OR a map for an array? Or maybe a clever mapped split?
Much thanks and happy Day of the Earth-Rodent-Forecaster!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: can u suggest a regex for this ?
by jo37 (Curate) on Feb 03, 2023 at 14:59 UTC | |
by misterperl (Friar) on Feb 03, 2023 at 15:26 UTC | |
by kcott (Archbishop) on Feb 03, 2023 at 17:04 UTC | |
by jo37 (Curate) on Feb 03, 2023 at 15:43 UTC | |
by hippo (Archbishop) on Feb 03, 2023 at 15:54 UTC | |
by misterperl (Friar) on Feb 03, 2023 at 16:24 UTC | |
by hippo (Archbishop) on Feb 03, 2023 at 17:10 UTC | |
| |
by Fletch (Bishop) on Feb 03, 2023 at 16:31 UTC | |
by marto (Cardinal) on Feb 04, 2023 at 14:47 UTC | |
by misterperl (Friar) on Feb 03, 2023 at 16:20 UTC | |
by jo37 (Curate) on Feb 03, 2023 at 16:31 UTC | |
|
Re: can u suggest a regex for this ?
by hv (Prior) on Feb 03, 2023 at 15:26 UTC | |
|
Re: can u suggest a regex for this ?
by Discipulus (Canon) on Feb 03, 2023 at 15:49 UTC | |
|
Re: can u suggest a regex for this ?
by tybalt89 (Monsignor) on Feb 03, 2023 at 17:32 UTC |