http://www.perlmonks.org?node_id=221176


in reply to Re: Re: count sort & output
in thread count sort & output

Of course I agree that counting letters should be done with tr, so assuming that the strings could be longer and are not required to be of the same length, you're code has a potential subtle bug.

If both "yahoo.com" and "yahoo.commies.org" are to be searched for, its chances of finding the second are dependent on its position in the regular expression relative to the first. You would have to sort your strings by length and build your regular expression with the longest ones first.

My code has its own sort of bug(?) in that both "www.yahoo.com" and "yahoo.com" might be found in the string "www.yahoo.commies.org"... however, it works correctly given the poorly stated requirements.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Re: count sort & output
by Anonymous Monk on Dec 19, 2002 at 22:21 UTC
    Your code has a potential subtle bug. You would have to sort your strings by length and build your regular expression with the longest ones first.

    You are of course right about the bug lurking in that snippet (sorting the strings in reverse order will fix this). My point was to let the regex engine do its work instead of the nested loop. That, and the suggestion to store intermediate results that would make for slowness.