Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Optimizing a string processing sub

by Dr. Mu (Hermit)
on Jan 09, 2003 at 18:25 UTC ( [id://225597]=note: print w/replies, xml ) Need Help??


in reply to Optimizing a string processing sub

I'm not sure this is any faster for the two-word case. It does seem simpler, though:
use strict; while (<DATA>) { chomp; print "$_: ", compare(split / /, $_), " in common.\n" } sub compare { my ($word1, $word2) = @_; my $n = length($word2); $word2 =~ s/$_// foreach split //, $word1; return $n - length($word2) } __DATA__ perl monk help temp frood hoopy bilbo baggins jibber jaber aabcc abbbc
It works by cycling through $word1 one character at a time and removing the first occurance of that character from $word2. When finished, $word2 will be shorter by the number of matches with $word1.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-18 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found