Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Testing some of the existing answers...

use v5.14; use Test::More; use Benchmark qw(:all); use IO::Callback; my @I; package RNO { use Moo::Role; requires 'replace_nth_comma'; my @gamut = ( ['a|b,c,d', 1, 'a,b,c,d'], ['a,b|c,d', 2, 'a,b,c,d'], ['a,b,c|d', 3, 'a,b,c,d'], ['aa,bb,cc|dd', 3, 'aa,bb,cc,dd'], ['|,,', 1, ',,,'], [',|,', 2, ',,,'], [',,|', 3, ',,,'], [',,,', 4, ',,,'], ); sub test_count { scalar @gamut }; sub test { my $class = shift; my $passes; for my $t (@gamut) { my ($target, @args) = @$t; my $result = $class->replace_nth_comma(@args); $passes += ::is($result, $target, "$class, target $target" +); } return $passes; } sub bench { my $class = shift; for my $count (0..63) { for my $t (@gamut) { my ($target, @args) = @$t; my $result = $class->replace_nth_comma(@args); } } } } # OP package RNO_1004836 { use Moo; with 'RNO'; push @I, __PACKAGE__; sub replace_nth_comma { my (undef, $count, $str) = @_; $count++; # off by one $str =~ s/(,)/--$count == 1 ? "|":$1/ge; return $str; } } # AnomalousMonk package RNO_1004848 { use Moo; with 'RNO'; push @I, __PACKAGE__; sub replace_nth_comma { my (undef, $count, $str) = @_; $count--; # off by one, other way $str =~ s{ (?: , [^,]*){$count} \K , }{|}xms; return $str; } } # Kenosis package RNO_1004858 { use Moo; with 'RNO'; push @I, __PACKAGE__; sub replace_nth_comma { my (undef, $count, $str) = @_; $str =~ s/(,)/!--$count ? '|' : $1/ge; return $str; } } # choroba package RNO_1004892 { use Moo; with 'RNO'; push @I, __PACKAGE__; sub replace_nth_comma { my (undef, $count, $str) = @_; my $pos = '0E0'; # plain 0 means the string begins with $from while ($count-- and $pos >= 0) { $pos = index $str, q(,), $pos eq '0E0' ? $pos : $pos + 1; } substr $str, $pos, 1, q(|) if $pos > 0; return $str; } } # trizen package RNO_1004895 { use Moo; with 'RNO'; push @I, __PACKAGE__; sub replace_nth_comma { my (undef, $count, $str) = @_; while ($str =~ /,/g) { if (--$count == 0) { substr($str, $-[0], $+[0] - $-[0], '|'); last; } } return $str; } } # grizzley package RNO_1004906 { use Moo; with 'RNO'; push @I, __PACKAGE__; sub replace_nth_comma { my (undef, $count, $str) = @_; $count--; # off by one, other way $str =~ s/((,.*?){$count}),/$1|/; return $str; } } # LanX package RNO_1005026 { use Moo; with 'RNO'; push @I, __PACKAGE__; sub replace_nth_comma { no warnings; my (undef, $count, $str) = @_; my @str= split /,/, $str; $str = join( "," , @str[0..$count-1] ) . "|" . join ( "," , @s +tr[$count..$#str] ); return $str; } } my @passed; for (@I) { push @passed, $_ if $_->test == $_->test_count; } done_testing; select(IO::Callback->new('>', sub { my $str = shift; $str =~ s/^/# /m; print STDOUT $str; })); say "Passed: @passed"; say "Benchmarks..."; cmpthese(-1, { map { my $class = $_; $class => sub { $class->bench } } + @passed });

The fastest seems to be Re: Replace the nth occurence by trizen.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Replace the nth occurence (testing and benchmarks) by tobyink
in thread Replace the nth occurence by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2014-08-01 10:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Who would be the most fun to work for?















      Results (0 votes), past polls