Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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
尝试使用foreach循环和push编写perl程序,但代码无法运行_Perl - Fatal编程技术网

尝试使用foreach循环和push编写perl程序,但代码无法运行

尝试使用foreach循环和push编写perl程序,但代码无法运行,perl,Perl,请告诉我如何更正。您使用的是素数而不是素数: ./listprime: line 3: use: command not found ./listprime: line 5: syntax error near unexpected token `(' ./listprime: line 5: `my @primes =(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);' 您缺少listprimes for循环的结束括号。(在返回语句之前) 您正在打印f而不

请告诉我如何更正。

您使用的是素数而不是素数:

./listprime: line 3: use: command not found
./listprime: line 5: syntax error near unexpected token `('
./listprime: line 5: `my @primes =(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);'
您缺少listprimes for循环的结束括号。(在返回语句之前)

您正在打印f而不是$f:

foreach $a(@prime) {

这将使它“工作”,尽管它可能更干净。

您收到的这些错误不是由
perl
产生的;它们由
bash
生成。这意味着您正在以某种方式将脚本输入shell

可能需要使文件可执行(例如,
chmod 700 listprime

还要确保
#是文件的前两个字符。(
od-c listprime | head-n1
应给出
0000000#!/u s r/b i n/p e r\n

如果一切正常,可以使用
/listprime
执行脚本


在您解决了这个问题之后,您的程序中有许多问题需要解决

print("f\n");
#/usr/bin/perl
使用strict;+-始终使用此选项,因为它会发现许多错误,
使用警告;编辑1
此编辑基于您更新的帖子,并列出了以下一些错误:

#!/usr/bin/perl
use strict;

my @primes =(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);

sub listprimes {
    my $n = shift;

    my @answer = ();

    foreach $a(@prime) {
        if($a<=$n){push @answer,$a;}

    return @answer;
}

sub random {
    my($a,$b) = @_;
    return int(rand($b-$a+1))+$a;
}

my $a = random(10,50);
my $f = listprimes($a);

print("f\n");
#!/usr/bin/perl
use strict;                             +- Always use this as it finds many errors,
use warnings;                         <-+  although it woulnd't have helped you this time.

my @primes =(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);

sub listprimes {
    my $max = shift;                  <--- Try to use meaningful names.

    my @answer;                       <--- New arrays are already empty.

    foreach my $prime (@primes) {     <--- @primes was misspelled.
        if($prime<=$max){push @answer,$prime;}
    }                                 <--- This curly was missing. Proper indenting not 
                                           only makes code easier to read; it makes errors
    return @answer;                        like this obvious.
}

sub random {
    my($min,$max) = @_;               <--- Avoid using $a and $b as they are semi-special.
    return int(rand($max-$min+1))+$min;
}

my $max = random(10,50);
my @selected = listprimes($max);      <--- Use an array to store multiple values.

print("@selected\n");                 <--- Sigil was missing.
产生这些错误是因为您似乎在启动脚本时将其视为shell脚本而不是Perl脚本。例如:

./listprime: line 3: use: command not found
./listprime: line 5: syntax error near unexpected token `('
./listprime: line 5: `my @primes =(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47)'
请注意,我的错误看起来有所不同,因为我使用的是Zsh,而您使用的是Bash,但仍然是同一个问题。与此相反,您应该使用Perl解释器,如下所示:

➜  /tmp  zsh test.pl
test.pl:2: command not found: use
test.pl:4: command not found: 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
test.pl:4: command not found: my
test.pl:6: command not found: sub
test.pl:7: command not found: my
test.pl:11: parse error near `$a'
如果您的脚本已经是可执行的(使用
chmod 755
添加执行权限),那么您可以将其作为
/script.pl
和第一个
#启动/usr/bin/perl
行应该处理其余部分

原始响应 这是对我发现的代码问题的逐步解释

需要改变 您的代码有几个编译问题,甚至无法运行。使用命令
perl-c
发出了几个问题,您可以重现这些问题,需要花时间来理解

下面是
listprimes
函数的问题。请注意我在下面的代码和
中所做的评论-----我用来标记它们的标记:

➜  /tmp  perl test.pl
12
示例输出

!/usr/bin/perl
use strict;

my @primes =(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);

sub listprimes {
        my $n = shift;

        my @answer = ();

        foreach $a (@primes) {
                if( $a<=$n ){ push @answer, $a; }
        }
        return @answer;
}

sub random {
        my($a,$b) = @_;
        return int(rand($b-$a+1))+$a;
}

my $a = random(10, 50);
my $f = listprimes($a);

print("$f\n");
补充建议

你应该考虑使用所有变量描述的名称。变量名(如

$a
)不提供有关所述变量内容的任何信息。例如,包含名称的数组
@strings
最好命名为
@names
(注意复数形式)

具有多个“单词”(例如,
listprimes
)的函数名可以更好地理解为
get\u primes
list\u primes
。考虑到英语的主语-动词-宾语结构,我倾向于使用动词-宾语格式作为函数名,因为它读起来更自然。换句话说,对于被视为主题的任何给定脚本或模块,函数名
get\u primes
primes\u get
读起来更好(除非您尝试像yoda那样说话,这是其他程序员不太可能欣赏的)

在决定变量和函数的单数或复数名称时,应考虑类型。例如,标量变量只能包含一个值。因此,单数名称(例如,
$max\u count
)是合适的。另一方面,数组类型可以包含多个值,因此复数名称(例如
@addresses
)更合适


这同样适用于函数。如果函数返回单个标量值,使用单数名称(例如
get\u max\u count
)会更好,但如果它返回数组(例如
get\u addresses
),则将其复数化会更好。如果您始终如一地这样做,您将为代码的用户提供更多信息,世界将变得更美好:)

请查看。你想要什么样的行为?布瑞恩,请记住考虑接受你对你最有用的答案,因为它对你和整个社会都有好处。有关更多信息,请参阅。我更改了它,但它也不起作用/listprime:第3行:未找到use:命令。/listprime:第5行:意外标记附近的语法错误
('./listprime:第5行:
my@primes=(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47)“很抱歉,在for循环的末尾需要一个括号,而不是子例程。@Brian,您正在以某种方式将脚本提供给shell。也许您需要使该文件可执行(例如,
chmod 700 listprime
)正如你所说,我更改了所有代码。当我运行代码时,它有相同的命令before@Brian,您是否阅读了我的评论并执行了
chmod 700 listprime
?还要确保
#!
是文件的前两个字符。
!/usr/bin/perl
use strict;

my @primes =(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);

sub listprimes {
        my $n = shift;

        my @answer = ();

        foreach $a (@primes) {
                if( $a<=$n ){ push @answer, $a; }
        }
        return @answer;
}

sub random {
        my($a,$b) = @_;
        return int(rand($b-$a+1))+$a;
}

my $a = random(10, 50);
my $f = listprimes($a);

print("$f\n");
➜  /tmp  ./test.pl
10