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

Re: Improve processing time for string substitutions

by kyle (Abbot)
on Apr 16, 2007 at 13:47 UTC ( [id://610353]=note: print w/replies, xml ) Need Help??


in reply to Improve processing time for string substitutions

Off the top of my head:

my $entity = join '|', keys %entitylist; $string =~ s/($entity)/$entitylist{$1}/g;

This way you only scan the whole string once instead of once for each entity.

Update: I've gotten a private message that this needs a /e switch to work. I submit that this is not the case.

my %entitylist = ( a => 1, b => 2); my $string = 'abc'; my $entity = join '|', keys %entitylist; $string =~ s/($entity)/$entitylist{$1}/g; print $string, "\n"; __END__ 12c

(I actually tested this before my original post, but I only pasted in the relevant portion.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found