Daniel Șuteu

NAME

Search::ByPrefix - An efficient, tree-based, multi-match prefix searcher.

VERSION

Version 0.02

SYNOPSIS

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

METHODS

new

Creates and returns a new object.

    my $sbp = Search::ByPrefix->new(%opt);

Where %opt can have the following keys:

  • table => {}

    The value of the table must be a multidimensional hash-like data structure.

add

    $sbp->add($key, $value);

The $key must be an array, where its granularity controls the matching.

    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.

    my $prefix = ['f','o'];
    my @values = $sbp->search($prefix);       # finds: ('foo-bar')

or:

    my $prefix = ['my', 'dir'];
    my @values = $sbp->search($prefix);       # finds: ('my/dir/path')

EXAMPLE

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"

AUTHOR

Daniel Șuteu, <trizenx at gmail.com>

BUGS

Please report any bugs or feature requests to https://github.com/trizen/Search-ByPrefix. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Search::ByPrefix

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright 2016-2017 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