【发布时间】:2019-10-11 21:09:50
【问题描述】:
我正在尝试在 Perl 中使用 while 循环,该循环从命令行获取一个参数,即要在字符串上打印的 hello 的数量。我写了以下内容:
# Basic settings to catch errors
use strict;
use warnings;
# Define subroutine containing the program
sub WhileNumbers {
# read script arguments
my $numberOfhellos = @ARGV;
chomp($numberOfhellos);
# Loop through the files
my $counts = 1;
while($counts <= $numberOfhellos) {
# Get user input the protein of interest
print ("Hello number $counts \n");
$counts ++;
}
}
# Calling the subroutine
WhileNumbers();
当我跑步时:
$ perl hellos.pl 3
我得到输出:
你好 1 号
虽然实际上我想:
你好 1 号
你好 2 号
你好 3 号
知道为什么 while 循环没有按预期工作吗?
【问题讨论】:
标签: perl parameter-passing conditional-statements command-line-arguments