xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/abbrev.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate;# Usage:
2*0Sstevel@tonic-gate;#	%foo = ();
3*0Sstevel@tonic-gate;#	&abbrev(*foo,LIST);
4*0Sstevel@tonic-gate;#	...
5*0Sstevel@tonic-gate;#	$long = $foo{$short};
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gate#
8*0Sstevel@tonic-gate# This library is no longer being maintained, and is included for backward
9*0Sstevel@tonic-gate# compatibility with Perl 4 programs which may require it.
10*0Sstevel@tonic-gate#
11*0Sstevel@tonic-gate# In particular, this should not be used as an example of modern Perl
12*0Sstevel@tonic-gate# programming techniques.
13*0Sstevel@tonic-gate#
14*0Sstevel@tonic-gate# Suggested alternative: Text::Abbrev
15*0Sstevel@tonic-gate#
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gatepackage abbrev;
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gatesub main'abbrev {
20*0Sstevel@tonic-gate    local(*domain) = @_;
21*0Sstevel@tonic-gate    shift(@_);
22*0Sstevel@tonic-gate    @cmp = @_;
23*0Sstevel@tonic-gate    local($[) = 0;
24*0Sstevel@tonic-gate    foreach $name (@_) {
25*0Sstevel@tonic-gate	@extra = split(//,$name);
26*0Sstevel@tonic-gate	$abbrev = shift(@extra);
27*0Sstevel@tonic-gate	$len = 1;
28*0Sstevel@tonic-gate	foreach $cmp (@cmp) {
29*0Sstevel@tonic-gate	    next if $cmp eq $name;
30*0Sstevel@tonic-gate	    while (@extra && substr($cmp,0,$len) eq $abbrev) {
31*0Sstevel@tonic-gate		$abbrev .= shift(@extra);
32*0Sstevel@tonic-gate		++$len;
33*0Sstevel@tonic-gate	    }
34*0Sstevel@tonic-gate	}
35*0Sstevel@tonic-gate	$domain{$abbrev} = $name;
36*0Sstevel@tonic-gate	while ($#extra >= 0) {
37*0Sstevel@tonic-gate	    $abbrev .= shift(@extra);
38*0Sstevel@tonic-gate	    $domain{$abbrev} = $name;
39*0Sstevel@tonic-gate	}
40*0Sstevel@tonic-gate    }
41*0Sstevel@tonic-gate}
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate1;
44