Search::ByPrefix - An efficient, tree-based, multi-match prefix searcher.
Version 0.04
Search::ByPrefix works by creating an internal table from a list of key/value pairs, where each key is an array.
Then, this table can be efficiently searched with an array prefix-key, which finds and returns all the values that have this certain prefix.
use Search::ByPrefix; my $sbp = Search::ByPrefix->new; # Add an entry $sbp->add($key, $value); # where $key is an array # Search by a prefix my @matches = $sbp->search($prefix); # where $prefix is an array
Creates and returns a new object.
my $sbp = Search::ByPrefix->new(%opt);
Where %opt can have the following keys:
%opt
table => {}
The value of the table must be a multidimensional hash-like data structure.
table
Starting with version 0.04, using a DBM::Deep database is also supported.
special_key => "\0\0\1\0\0"
Special unique key, used internally to store the original values.
$sbp->add($key, $value);
The $key must be an array, where its granularity controls the matching.
$key
my $key = ['f','o','o','-','b','a','r']; my $value = 'foo-bar'; $sbp->add($key, $value);
or:
my $key = ['my', 'dir', 'path']; my $value = 'my/dir/path'; $sbp->add($key, $value);
my @matches = $sbp->search($prefix);
Searches and returns a list of values that have a certain prefix, where each value is the original value associated with the matched key.
The $prefix must be an array, where its granularity controls the matching.
$prefix
my $prefix = ['f','o']; my @values = $sbp->search($prefix); # finds: ('foo-bar')
my $prefix = ['my', 'dir']; my @values = $sbp->search($prefix); # finds: ('my/dir/path')
This example illustrates how to add some key/value pairs to the table and how to search the table with a given prefix:
use 5.010; use Search::ByPrefix; my $obj = Search::ByPrefix->new; sub make_key { [split('/', $_[0])] } foreach my $dir ( qw( /home/user1/tmp/coverage/test /home/user1/tmp/covert/operator /home/user1/tmp/coven/members /home/user2/tmp/coven/members /home/user1/tmp2/coven/members ) ) { $obj->add(make_key($dir), $dir); } # Finds the directories that have this common path say for $obj->search(make_key('/home/user1/tmp'));
The results are:
"/home/user1/tmp/coverage/test" "/home/user1/tmp/covert/operator" "/home/user1/tmp/coven/members"
https://github.com/trizen/Search-ByPrefix
Daniel Șuteu, <trizen at cpan.org>
<trizen at cpan.org>
Copyright 2016-2022 Daniel Șuteu.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
To install Search::ByPrefix, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Search::ByPrefix
CPAN shell
perl -MCPAN -e shell install Search::ByPrefix
For more information on module installation, please visit the detailed CPAN module installation guide.