Note that if you need to disambiguate between two strings that match for the first few characters you can require some number of characters thus:

Or else one may proceed like thus:

my @matching=grep { 0 == index $_, $userinput } @commands;

Then take actions according to whether @matching==0, @matching==1 or @matching>=2. In the latter case depending on what the commands are actually supposed to do I would either emit a warning and decide upon priority (which may be given by the ordering of @commands itself) or ask the users to refine their input.

Update: minimal example follows.

#!/usr/bin/perl use strict; use warnings; my @commands=qw/foo bar baz/; print "Choose command [@commands]\n"; { chomp(my $cmd=<STDIN>); my @ok=grep { 0 == index $_, $cmd } @commands; print "You chose [@ok]\n" and last if @ok==1; warn +(@ok>1 ? "Ambiguos command [@ok]" : "Invalid command"), ", retry!\n"; redo; } __END__

Usage example:

$ ./foo.pl Choose command [foo bar baz] fred Invalid command, retry! fo You chose [foo] $ ./foo.pl Choose command [foo bar baz] ba Ambiguos command [bar baz], retry! bar You chose [bar]

In reply to Re^2: matching a regular expression by blazar
in thread matching a regular expression by s_gaurav1091

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.