Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

A question a Tree::Trie

by jhuijsing (Acolyte)
on Oct 18, 2017 at 05:08 UTC ( [id://1201548]=perlquestion: print w/replies, xml ) Need Help??

jhuijsing has asked for the wisdom of the Perl Monks concerning the following question:

I have Tree:Trie created when I do a prefix search I am not getting the response I expect

here is the trie

0 Tree::Trie=HASH(0x600d98df8) '_DEEPSEARCH' => 3 '_END' => '' '_FREEZE_END' => 0 '_MAINHASHREF' => HASH(0x600d99050) '+' => HASH(0x600d9cfa8) 6 => HASH(0x600d9d0c8) 1 => HASH(0x600d9d008) '' => 'Term' 1 => HASH(0x600d9d2d8) 4 => HASH(0x600d9d188) 1 => HASH(0x600d9cea0) 1 => HASH(0x600d9d200) '' => 'Outleg_1' 0 => HASH(0x600d9d170) '' => 'Outleg_2' 5 => HASH(0x600d9d458) '' => 'Term_Ref' 'd' => HASH(0x600d9d0b0) 'e' => HASH(0x600d9d260) 'f' => HASH(0x600d98f78) 'a' => HASH(0x600d9d578) 'u' => HASH(0x600d983d8) 'l' => HASH(0x600d9d5a8) 't' => HASH(0x600d9d5d8) '' => 'Unknown'
  • do a search for +6114110
  • x $trie->lookup( "+6114110" )

    0 '+6114110'

  • do a search for +61141102
  • x $trie->lookup( "+61141102" )

    empty array

    I was expecting to get the same result, As it is the best prefix that matches

    Or I have got it wrong?

    Replies are listed 'Best First'.
    Re: A question a Tree::Trie
    by Anonymous Monk on Oct 18, 2017 at 06:11 UTC
      Hi, what is that in code tags you have posted? How about runnable code?

        from the documentation prefix: Will return the longest entry in the trie that is a prefix of word.

        usr/bin/perl use Tree::Trie; my $trie = Tree::Trie->new( { deepsearch => 'prefix' } ); $trie->add_data ("+61" => "Term", "+611411" => Outleg_1, "+6114110" +=> "Outleg_2" , "+65" => "Term_Ref", default => unknown ); $trie->lookup("+61"); $trie->lookup("+612");

          Hello jhuijsing,

          I think you missed this from the documentation:

          $trie->lookup(word, suffix_length)
          This method performs lookups on the trie. In list context, it returns a complete list of words in the trie which begin with word. In scalar context, the value returned depends on the setting of the 'deepsearch' option.

          So, the deepsearch option has effect only in scalar context:

          use strict; use warnings; use Tree::Trie; my $trie = Tree::Trie->new( { deepsearch => 'prefix' } ); $trie->add_data ( '+61' => 'Term', '+611411' => 'Outleg_1', '+6114110' => 'Outleg_2', '+65' => 'Term_Ref', default => 'unknown', ); my $result = $trie->lookup('+612'); print "+612: $result\n";

          Output:

          17:18 >perl 1830_SoPW.pl +612: +61 17:18 >

          But when you assign the result of lookup() to an array, you put the function call into list context, and the deepsearch option does not apply.

          Hope that helps,

          Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

    Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others pondering the Monastery: (6)
    As of 2024-04-23 12:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found