这是我想出的,它允许您有一个用于查询输入的字符串并检查以确保响应有效。如果没有,它将再次查询用户。该子例程返回所选数组项的索引。
sub displayMenu($@)
{
# First item is the query string, so shift it
# from the inputs and save it
my $queryString = shift @_;
# Loop control variable;
my $lcv;
# User selection of choices
my $selection;
# Flag to indicate you have the correct input
my $notComplete = 1;
# Clear some space on the screen
print "\n" x 10;
# Loop until you have an answer
while ( $notComplete )
{
print "-" x 40 . "\n";
for( $lcv = 1; $lcv <= scalar(@_) ; $lcv++ )
{
printf " %4d) %s\n",$lcv,$_[$lcv-1];
}
print "\n";
# Query for a response
print "$queryString\n";
# Get response
$selection = <STDIN>;
# Remove the carriage return
chomp($selection);
# Check to make sure it is string of digits
# and it is within the range of the numbers
# If it is, clear the not complete flag
if (( $selection =~ m/^\d*/ ) &&
( 0 < $selection ) && ( scalar(@_) >= $selection))
{
$notComplete = 0;
}
# Else there is a error so try again
else
{
print "\n" x 10;
print "\nIncorrect Input. Try again\n";
}
}
# Return the index of the selected array item
return ($selection - 1);
}
如何调用它的示例如下:
$returnValue = displayMenu("Enter number of the item you want to select",("Test1","Test2","Test3"));
调用中的第一项是要为选择的输入打印的字符串,然后是要从中选择的项数组。然后它从选择中返回索引。
从下面的评论中回答您的问题。我的回答是很想发表评论。
如果您将其分解为printf " %4d) %s\n",$lcv,$_[$lcv-1]
多个部分,则 printf 用于格式化输出的功能。print if 的第一个参数是一个字符串,指示行的格式,后跟提供要格式化的值的项目。在这种情况下,%4d 是打印出一个整数,它应该在行上占据 4 个空格,而 %s 是打印出一个字符串。接下来的项目是格式说明符的参数,在这种情况下是 $lcv是选项的编号 (%4d) 并且$_[$lcv-1]
是选项($lcv-1 是因为数组在从零开始的索引中,$_ 是访问传递给例程的参数。注意:我移动了第一个参数为 %s 传入的项以获得标题)。如果您查看http://perldoc.perl.org/functions/sprintf.html它给出了各种格式说明符的描述(sprintf 是打印到一个字符串,但格式说明符对于 printf 是相同的)。
这( 0 < $selection ) && ( scalar(@_) >= $selection))
是为了确保输入在给定的选择范围内。选择应该大于零并且小于或等于选择的项目数,这是scalar(@_)
返回的(@_
指的是传递给例程的参数,标量函数返回数组中的项目数)。