Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Fast Replacement

by muba (Priest)
on Jun 14, 2013 at 05:21 UTC ( #1038886=note: print w/replies, xml ) Need Help??


in reply to Fast Replacement

The reason it is slow is that for every call to index, Perl has to go through the process of checking each character in the string whether it is an "!". Again and again and again. So you could use the $index variable to tell Perl not to bother about the characters it has already checked:

while ( index($group, "!", $index) > -1 and $index<50000 ) {

Alternatively, because TIMTOWTDI:

use strict; use warnings; my $string = "abc!def!ghi!jkl!mno!pqr!stu!vwx!yz"; my $limit = 3; $string = join("\n", split(/!/, $string, $limit)); print $string;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2020-05-24 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (142 votes). Check out past polls.

    Notices?