Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: search and replace frustration

by ikegami (Pope)
on Jun 06, 2005 at 14:51 UTC ( #463929=note: print w/replies, xml ) Need Help??


in reply to search and replace frustration

Since you're searching for a constant string, index is faster than a regexp:

foreach my $elem (@repos) { my $repo_name = $elem; my $pos = index(lc($repo_name), lc($svnroot)); if ($pos >= 0) { substr($repo_name, $pos, length($svnroot), ''); } print("$repo_name\n"); }

Or since you probably meant to search for /^\Q$svnroot\E/ (i.e. search for $svnroot at the beginning of the string), this is more precise and even faster:

foreach my $elem (@repos) { if (lc($svnroot) eq substr(lc($elem), 0, length($svnroot))) { print(substr($elem, length($svnroot)), "\n"); } else { print("$elem\n"); } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-05-31 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (175 votes). Check out past polls.

    Notices?