用于创建两个数组的 Perl 脚本

发布于 2024-12-10 21:10:05 字数 678 浏览 0 评论 0原文

输入:命令行上的数字列表

输出:两个数字列表,一个输入数字大于零,一个输入数字小于零(忽略零值数字)

这是我的代码

#!/usr/bin/perl
$i++ = 0;
$j++ = 0;

while ($number = <>)
 {
        if($number<0)
          $first[$i++]=$number;
        else
          $second[$j++]=$number;

 }

 print "The numbers with value less than zero are\n";

  foreach $number (@first)
     print $number;
print "The numbers with value greater than zero are\n"

  foreach $number(@second)
     print $number;

我得到以下愚蠢的结果我无法纠正的错误。这些错误

divide.pl: 2: ++: not found
divide.pl: 3: ++: not found
divide.pl: 5: Syntax error: ")" unexpected

有人可以帮我纠正这些错误吗?我是 perl 脚本新手

Input: A list of numbers on command line

Output: Two lists of numbers ,one with input numbers that are greater than zero and one with those that are less than zero (Ignoring zero valued numbers)

here is my code

#!/usr/bin/perl
$i++ = 0;
$j++ = 0;

while ($number = <>)
 {
        if($number<0)
          $first[$i++]=$number;
        else
          $second[$j++]=$number;

 }

 print "The numbers with value less than zero are\n";

  foreach $number (@first)
     print $number;
print "The numbers with value greater than zero are\n"

  foreach $number(@second)
     print $number;

I am getting the following silly errors which i am not able to rectify.The errors are

divide.pl: 2: ++: not found
divide.pl: 3: ++: not found
divide.pl: 5: Syntax error: ")" unexpected

Can anybody help me out with rectifying these errors please? I am new to perl script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南七夏 2024-12-17 21:10:05

在 Perl 中,复合语句上的大括号不是可选的。

你的言论:

$i++=0;
$j++=0;

没有道理;您可能只想删除“++”。

您的打印语句之一缺少分号。

解决这些问题后,您应该

use strict;
use warnings;

#! 行之后添加。这会引入更多错误消息;你还需要修复这些问题。例如,您需要使用 my() 声明变量。

Curly braces on compound statements are not optional in Perl.

Your statements:

$i++=0;
$j++=0;

don't make sense; you probably just want to delete the "++".

You're missing a semicolon on one of your print statements.

Once you've got those problems fixed, you should add

use strict;
use warnings;

after the #! line. This will introduce more error messages; you'll need to fix those as well. For example, you'll need to declare your variables using my().

烟织青萝梦 2024-12-17 21:10:05

您提供的代码很难编译。循环应在主块周围有 {},数组最好使用 push(或 unshift)创建,您应该使用 strict 和警告,并且您不能在赋值的同时进行增量(例如 $i++ = 0)。

use v5.10;
use strict;
use warnings;

my (@first, @second);

while (<STDIN>) {  # <STDIN> clearer than <> in this case
    chomp;
    if ($_ < 0) {
        push @first, $_;
    } elsif ($_ > 0) {
        push @second, $_;
    }
}

say "Numbers less than zero:";
say "@first";
say "Numbers greater than zero:";
say "@second";

The code you present will hardly compile. Loops should have {} around the main block, arrays are better created with push (or unshift), you should use strict and warnings, and you can't do increments at the same time as assignments (e.g. $i++ = 0).

use v5.10;
use strict;
use warnings;

my (@first, @second);

while (<STDIN>) {  # <STDIN> clearer than <> in this case
    chomp;
    if ($_ < 0) {
        push @first, $_;
    } elsif ($_ > 0) {
        push @second, $_;
    }
}

say "Numbers less than zero:";
say "@first";
say "Numbers greater than zero:";
say "@second";
沉溺在你眼里的海 2024-12-17 21:10:05

我不知道 $i++ = 0 的含义,但将其更改为 $i = 0 来初始化变量。

另外,yuu 在 while 循环中应该做的第一件事是调用 chomp($number) 来删除虚假换行符 - 5\n 不是数字并将其视为一个会让 perl 感到困惑。

一旦你解决了这个问题,发布任何出现的新错误 - 但我没有看到任何其他问题。

I don't know what $i++ = 0 is supposed to mean, but change that to $i = 0 to initialize the variables.

Also, the first thing yuu should do in the while loop is call chomp($number) to remove spurious newlines - 5\n is not a number and treating it as one will confuse perl.

Once you've fixed that, post any new errors that show up - I don't see any other problems though.

ゝ杯具 2024-12-17 21:10:05

你如何执行这个 perl 脚本?除了提到的有关代码本身的错误之外。看起来您正在尝试使用 dash 而不是 perl 来评估代码。

如果您使用 Perl 执行它,您应该看到的错误如下:

无法修改 /tmp/foo.pl 标量赋值中的后增量 (++)
第 2 行,“0;”附近

但相反,您的错误更符合 dash 的输出:

$破折号/tmp/foo.pl

/tmp/foo.pl: 2: ++: 未找到

/tmp/foo.pl: 3: ++: 未找到

一旦你确认你正在正确运行你的perl脚本,你就可以可以开始解决人们在您的代码中提到的其他问题。最简单的方法是通过 perldivide.pl 运行它,而不是您正在执行的任何操作。

How are you executing this perl script? Beyond the errors mentioned about the code itself. It looks like you are attempting to evaluate the code using dash instead of perl.

The errors you should be seeing if you were executing it with Perl would be like:

Can't modify postincrement (++) in scalar assignment at /tmp/foo.pl
line 2, near "0;"

But instead, your errors are more in line with what dash outputs:

$ dash /tmp/foo.pl

/tmp/foo.pl: 2: ++: not found

/tmp/foo.pl: 3: ++: not found

Once you've verified that you are running your perl script properly you can start working through the other problems people have mentioned your code. The easiest way to do this is to run it via perl divide.pl instead of whatever you are doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文