Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Replace all characters inbetween

by Tux (Canon)
on Jun 05, 2022 at 08:28 UTC ( [id://11144405]=note: print w/replies, xml ) Need Help??


in reply to Replace all characters inbetween

TIMTOWTDI (and likely a lot more than just these three)

use 5.018003; use warnings; use DP; use Test::More tests => 3; use Benchmark "cmpthese"; my $n = 100; my $s = "AAAAAXXXXXXXAAAXXXXXAXXXXAAAAA" x 100; my $A = "A" x 10000; sub s_ger { $s =~ s{A\K([^A]+)(?=A)} {"A" x length + $1 }ger } sub substr_ger { $s =~ s{A\K([^A]{1,10000})(?=A)}{substr $A, 0, length + $1 }ger } sub split_join { my @s = split m/A\K([^A]+)(?=A)/ => $s; for ($_ = 1; $_ < $#s; $_ += 2) { $s[$_] = "A" x length $s[$_]; } join "" => @s; } is (s_ger (), "A" x 3000, "s//x/ger"); is (substr_ger (), "A" x 3000, "s//substr/ger"); is (split_join (), "A" x 3000, "split/join"); cmpthese (-2, { s_ger => \&s_ger, subst => \&substr_ger, spljn => \&split_join, });

->

$ perl test.pl 1..3 ok 1 - s//x/ger ok 2 - s//substr/ger ok 3 - split/join Rate spljn s_ger subst spljn 3330/s -- -2% -6% s_ger 3405/s 2% -- -4% subst 3550/s 7% 4% --

YMMV


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Replace all characters inbetween
by Anonymous Monk on Jun 05, 2022 at 09:53 UTC
    Hi Tux!
    Thank you so much for the help!! If I may, can you take out one of the solutions, because I have hard time isolating one?
    I tried this:
    my $s = "AAAAAXXXXXXXAAAXXXXXAXXXXAAAAA"; $s =~s{A\K([^A]+)(?=A)} {"A" x length $1 }; print $s."\n";
    but it does not print something proper.

      Just a general note for future reference: As a rule, "it doesn't work" is a very poor problem description. The code you posted is short enough that you could have listed its output and any warning/error messages, etc., as well. Please see Short, Self-Contained, Correct Example.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found