Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Optimizing a string processing sub

by boo_radley (Parson)
on Jan 08, 2003 at 20:57 UTC ( #225352=note: print w/replies, xml ) Need Help??

in reply to Optimizing a string processing sub

This is a variation on a faq -- see perlfaq 4, "How can I count the number of occurrences of a substring within a string?".
Using a little eval trickery :
foreach (<DATA>) { my ($w1, $w2) = split / /, $_; my %common = (); foreach ( split //, $w1){ eval "\$foo=\$w2=~tr/$_/$_/" unless exists $common{$_}; $common {$_}=1 if $foo; } print "$w1 and $w2 have ", join (", ",sort keys %common)," in comm +on\n"; } __DATA__ perl monk help temp frood hoopysdf bilbo baggins jibber jabber

I wonder about your algorithm in score2, though. It seems to return 1 regardless of actual matches because of the last. Perhaps you need a larger/ more complex word set?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2023-03-20 19:29 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.