Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Better way to search array for elements in other array?

by SuicideJunkie (Vicar)
on Jan 24, 2011 at 20:47 UTC ( [id://883997]=note: print w/replies, xml ) Need Help??


in reply to Better way to search array for elements in other array?

What if you take one list of strings and convert it to a HoHoHo...?

The top level key would be the first letter of each string. At each level, you have either the string to eq against, or a hash of choices for the next letter.

EG: my $tree = {f => {o => 'foo', i => {r => 'fire', n => 'fine', }, }, b => 'bar', }

You would know instantly that 'monk' isn't in the second list because !exists($tree->{m}).
You would know 'balloon' doesn't exist pretty quick because exists($tree->{b}) and ref($tree->{b}) ne 'HASH' and $tree->{b} ne 'balloon'.

If the second list is something constant without too much duplication early in the string, such as a dictionary, then it should work really well.

Replies are listed 'Best First'.
Re^2: Better way to search array for elements in other array?
by Limbic~Region (Chancellor) on Jan 24, 2011 at 21:49 UTC
    SuicideJunkie,
    Explain to me how that is going to efficiently tell me that:
    "Have a Merry Christmas and a Happy New Year" contains "and"

    The trouble is that it can appear anywhere in the string so you either need to build the data structure for every starting point in the string or repeat the test for as many characters as are in the string (unless I am missing something).

    Cheers - L~R

      I was under the impression that the array elements were entire strings... that the search was for stuff in the other array, rather than for stuff inside the strings in the other array.

      Given that, you're right that it is solving a completely different problem.

Re^2: Better way to search array for elements in other array?
by eff_i_g (Curate) on Jan 24, 2011 at 21:12 UTC
    I saw code for this recently in Find Words a la Boggle. You can find the spot by searching for "dictionary tree" in the code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found