3

我正在使用该Tree::Trie模块来构建关键字树。我想按原始数组中的顺序打印数组的元素。

use strict;
use warnings;

use Tree::Trie;

my @ar = ('foo', 'great','food', 'banana','apple', 'fish');

my ($trie) = new Tree::Trie;
$trie->add(@ar);
my @ms  = $trie->lookup('f');
print join(",", @ms);

输出是

fish,foo,food

期望的输出

foo,food,fish

有人可以帮助我吗?

4

1 回答 1

7

另一种选择是使用Tree::Trie's add_data,它会将您的话与订单数据一起使用,如下所示:

'foo' => 0,'great' => 1,'food' => 2,'banana' => 3,'apple' => 4,'fish' => 5

并在 a 之后返回带有相关数据的单词lookup_data。然后可以使用该数据来保留原始词序:

use strict;
use warnings;
use Tree::Trie;

my @ar = qw( foo great food banana apple fish );

my $trie = Tree::Trie->new();
$trie->add_data( map { $ar[$_] => $_ } 0..$#ar );

my %ms = $trie->lookup_data('f');
my @ms = sort { $ms{$a} <=> $ms{$b} } keys(%ms);
print(join(',', @ms), "\n");

输出:

foo,food,fish
于 2012-12-06T02:32:03.387 回答