summaryrefslogtreecommitdiff
path: root/lib/Text/Abbrev.pm
blob: d12dfb36a697daf84e24248e4cc64c4e43fb6d1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Text::Abbrev;
require 5.000;
require Exporter;

=head1 NAME

abbrev - create an abbreviation table from a list

=head1 SYNOPSIS

    use Abbrev;
    abbrev *HASH, LIST


=head1 DESCRIPTION

Stores all unambiguous truncations of each element of LIST
as keys key in the associative array indicated by C<*hash>.
The values are the original list elements.

=head1 EXAMPLE

    abbrev(*hash,qw("list edit send abort gripe"));

=cut

@ISA = qw(Exporter);
@EXPORT = qw(abbrev);

# Usage:
#	&abbrev(*foo,LIST);
#	...
#	$long = $foo{$short};

sub abbrev {
    local(*domain) = shift;
    @cmp = @_;
    %domain = ();
    foreach $name (@_) {
	@extra = split(//,$name);
	$abbrev = shift(@extra);
	$len = 1;
	foreach $cmp (@cmp) {
	    next if $cmp eq $name;
	    while (substr($cmp,0,$len) eq $abbrev) {
		$abbrev .= shift(@extra);
		++$len;
	    }
	}
	$domain{$abbrev} = $name;
	while (@extra) {
	    $abbrev .= shift(@extra);
	    $domain{$abbrev} = $name;
	}
    }
}

1;