Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: s///g within m//g regions

by LanX (Saint)
on Nov 09, 2021 at 13:25 UTC ( [id://11138612]=note: print w/replies, xml ) Need Help??


in reply to Re^2: s///g within m//g regions
in thread s///g within m//g regions

> but what about other "region" definitions, and possibly multiple regions in a line?

SSCCE please :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^4: s///g within m//g regions
by almr (Sexton) on Nov 09, 2021 at 13:32 UTC
    # turn * into - within |...| regions
    A |simple*example| is |so*simple| to come*up |with
    these*days|
    
    should become
    # turn * into - within |...| regions
    A |simple-example| is |so-simple| to come*up |with
    these-days|
    
    So, iterate over matches, and perform substs within each match.
      use strict; use warnings; use feature 'state'; use Test::More tests => 1; my $in = 'A |simple*example| is |so*simple| to come*up |with these*days|'; my $want = 'A |simple-example| is |so-simple| to come*up |with these-days|'; my $have = join '|', map { state $i = 0; $i++ % 2 and s/\*/-/g; $_ } split /\|/, $in, -1; is $have, $want;

      🦛

      that's very different because there are no lines in between.

      use strict; use warnings; use Test::More; my $in = 'A |simple*example| is |so*simple| to come*up |with these*days| according to some *experts*'; my $exp = 'A |simple-example| is |so-simple| to come*up |with these-days| according to some *experts*'; my $start = qr/\|/; my $stop = qr/\|/; my $got = $in; $got =~ s[ ($start) (.*?) ($stop) ][ "$1" . ( $2 =~ tr/\*/-/r ) . "$3" ]gsxe; is($got,$exp); done_testing;

      updates
      added
      • test
      • line-break

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      TIMTOWTDI

      use strict; use warnings; use Test::More; my $in = 'A |simple*example| is |so*simple| to come*up |with these*days| according to some *experts*'; my $exp = 'A |simple-example| is |so-simple| to come*up |with these-days| according to some *experts*'; my $start = qr/\|/; my $stop = qr/\|/; my $got; for ( split /($start|$stop)/s, $in) { if ( /$start/.../$stop/ ) { #print "<$_>"; s/\*/-/gs; } $got .= $_; } is($got,$exp); done_testing;

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        > TIMTOWTDI

        use strict; use warnings; use Test::More; my $in = 'A |simple*example| is |so*simple| to come*up |with these*days| according to some *experts*'; my $exp = 'A |simple-example| is |so-simple| to come*up |with these-days| according to some *experts*'; my $start = qr/\|/; my $stop = qr/\|/; my $got = join "", map { /$start/.../$stop/ ? s/\*/-/gr : $_ } split /($start|$stop)/s, $in; is($got,$exp); done_testing;

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^4: s///g within m//g regions
by almr (Sexton) on Nov 11, 2021 at 13:23 UTC
    Thanks for all the variants, I've learned some things I've missed before and re-learned some things I've forgotten (this seems to happen every time I don't use perl for a couple of months or so).

    The problem I see with SSCCE (despite its benefits) is that it encourages "particularization" instead of generalization; so after getting example-tailored responses, one has to play whack-a-mole with them to get to the original, abstract problem / "programming malaise".

    Then again, those example-tailored responses always teach new bits of ingenuity. Anyway, thanks again.

      The problem I see with SSCCE (despite its benefits) is that it encourages "particularization" instead of generalization ...

      See How to ask better questions using Test::More and sample data. Is incumbent upon the question asker to provide a suitable coverage of test cases to meet their requirements.


      🦛

      > encourages "particularization" instead of generalization;

      I like generalizations too.

      I think the variations I showed are as general as possible and cover all use cases I could imagine.

      Otherwise please provide an SSCCE ... ;-p

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138612]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found