in reply to Regex - Matching prefixes of a word
update: psini has already suggested this.
If you want to allow command words to be abbreviated to uniqueness, something like the following might appeal:
use strict; use warnings; my $word = shift; my @words = qw(lock tubes on raider dispersion offset fire all torpedo +es); my $pat = quotemeta($word); my @matches = grep (/^$pat/, @words); if(@matches == 0) { print "Unknown command \"$word\"\n"; } elsif(@matches == 1) { print "Recognized \"$word\" as command $matches[0]\n"; } else { print "Ambiguous command \"$word\" could be any of: @matches\n"; }
|
|---|