Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Perl在用户选项菜单的数组中使用不同的元素_Arrays_Perl_Data Structures_Menu - Fatal编程技术网

Arrays Perl在用户选项菜单的数组中使用不同的元素

Arrays Perl在用户选项菜单的数组中使用不同的元素,arrays,perl,data-structures,menu,Arrays,Perl,Data Structures,Menu,我有一个名为@option 每次运行脚本时,@选项可能包含不同的元素和不同数量的元素 第一次运行脚本时,它可能包含 狗、猫、羚羊、大象、猪 第二次运行脚本时,它可能包含 马、大象、山羊 我需要什么: 使用数组中的元素,通过键入数组元素字符串提示用户选择元素 或者输入数组中每个元素链接的值, 或者你能想到的其他更好的方法 例如: Please select which one you want to delete by entering its associated number: dog[1]

我有一个名为
@option

每次运行脚本时,
@选项
可能包含不同的元素和不同数量的元素

第一次运行脚本时,它可能包含

狗、猫、羚羊、大象、猪

第二次运行脚本时,它可能包含

马、大象、山羊

我需要什么: 使用数组中的元素,通过键入数组元素字符串提示用户选择元素 或者输入数组中每个元素链接的值, 或者你能想到的其他更好的方法

例如:

Please select which one you want to delete by entering its associated number:
dog[1] 
cat[2] 
antelope[3]
elephant[4]
pig[5]
(在用户选择一个后,我的代码的其余部分将执行一些操作,然后将其删除)

我知道我可以用STDIN匹配狗做这个,匹配大象做那个等等来做这个


<> P><强> >对不同方法的建议,人们认为是最好的/最有效的/最可接受的/专业的/优选的/聪明的方式。

< P>你需要做一些验证。

for my $id (1 .. scalar @options) {
  my $element = $options[$id - 1];
  say "$id: $element";
}
my $input = <STDIN>;
my $selection = $options[$input - 1];
用于我的$id(1..scalar@options){
my$element=$options[$id-1];
说“$id:$element”;
}
我的$input=;
my$selection=$options[$input-1];

我建议

sub get_user_selection {
  my(@choices) = @_;
  my $index = 1;
  for my $choice (@choices) {
    print "$index.) $choices[$index - 1]\n";
    $index++;
  }
  print "Enter your choice: ";
  return $choices[<STDIN> - 1];
}
子获取用户选择{
我的(@选择)=@;
我的$index=1;
对于我的$choice(@choices){
打印“$index.)$choices[$index-1]\n”;
$index++;
}
打印“输入您的选择:”;
返回$choices[-1];
}

此子例程将显示一个菜单,并返回用户通过输入数字选择的值。

这是我想到的,它允许您使用字符串查询输入,并进行检查以确保响应有效。如果没有,它将再次查询用户。子例程返回所选数组项的索引

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);
}
其中,调用中的第一项是要打印的字符串,用于选择的输入,然后是要从中选择的项的数组。然后从选择返回索引

从下面的评论中回答您的问题。我的回答是很想发表评论

如果将
printf“%4d)%s\n”、$lcv、$\u[$lcv-1]
分解为多个部分,则printf用于格式化输出的函数。打印if的第一个参数是一个字符串,指示行的格式,后跟提供要格式化的值的项。在这种情况下,%4d将打印出一个整数,它应在行上占据4个空格,%s将打印出一个字符串。接下来的项目是格式说明符的参数,在这种情况下,$lcv是选择的编号(%4d),而
$\u[$lcv-1]
是选择($lcv-1是因为基于零的索引中的数组和$\是用来访问传递给例程的参数的。注意:我移动了传入项的第一个参数以获得标题)用于%s。如果您查看它,它会给出各种格式说明符的描述(sprintf将打印为字符串,但格式说明符与printf相同)


(0<$selection)和&(scalar(@)>=$selection))
用于确保输入在给定的选择范围内。选择值应大于零,小于或等于包含选项的项数,即
scalar(@/code>返回的项数(
@
指传入例程的参数,标量函数返回数组中的项数).

对这样的问题要小心——它们有被关闭的危险,并被归类为“不具建设性”:目前来看,这个问题不适合我们的问答形式。我们希望答案能得到事实、参考资料或特定专业知识的支持,但这个问题可能会引发辩论、争论、投票或延伸“你看了菜单驱动系统吗?”Jonathan Leffler -是的,我看了CPAN,那里有很多解决方案,但理想的是,我在寻找最好的方法,不用使用模块,也谢谢你的建议。谢谢,这正是我要找的东西,你认为这是首选的方法吗?如果这样做的话,或者只是众多选项中的一个?当你提到“你需要做一些验证”时,你指的是什么?谢谢。这是尽可能基本的——迭代数组中的元素数,打印出id和元素数,然后通过索引获得选择。(移动1,因为索引从0开始计数,ID通常从1开始计数)例如,他们没有输入“asdf”,如果是的话,他们会处理一个重发。谢谢,刚刚回答了我的问题,我现在明白你的意思了。谢谢,这也是我一直在寻找的东西,我会让他们两个都尝试一下。非常感谢你的回答,这正是我所需要的。你能更详细地向我解释一下什么吗请使用以下行,它们是“printf”%4d)%s\n“、$lcv、$\u[$lcv-1];”和“(0<$selection)&&(scalar(@\u)>=$selection))’,因为我有点不确定,谢谢你的帮助我的答案不适合评论,所以我把它添加到上面的答案中。很高兴能提供帮助,而且对你很有用。非常感谢你帮了我很多忙,你解释得很好,再次感谢。
$returnValue = displayMenu("Enter number of the item you want to select",("Test1","Test2","Test3"));