用于创建两个数组的 Perl 脚本
输入:命令行上的数字列表
输出:两个数字列表,一个输入数字大于零,一个输入数字小于零(忽略零值数字)
这是我的代码
#!/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Perl 中,复合语句上的大括号不是可选的。
你的言论:
没有道理;您可能只想删除“++”。
您的打印语句之一缺少分号。
解决这些问题后,您应该
在
#!
行之后添加。这会引入更多错误消息;你还需要修复这些问题。例如,您需要使用my()
声明变量。Curly braces on compound statements are not optional in Perl.
Your statements:
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
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 usingmy()
.您提供的代码很难编译。循环应在主块周围有
{}
,数组最好使用push
(或unshift
)创建,您应该使用 strict 和警告,并且您不能在赋值的同时进行增量(例如$i++ = 0
)。The code you present will hardly compile. Loops should have
{}
around the main block, arrays are better created withpush
(orunshift
), you should use strict and warnings, and you can't do increments at the same time as assignments (e.g.$i++ = 0
).我不知道
$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.
你如何执行这个 perl 脚本?除了提到的有关代码本身的错误之外。看起来您正在尝试使用
dash
而不是perl
来评估代码。如果您使用 Perl 执行它,您应该看到的错误如下:
但相反,您的错误更符合
dash
的输出:一旦你确认你正在正确运行你的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 ofperl
.The errors you should be seeing if you were executing it with Perl would be like:
But instead, your errors are more in line with what
dash
outputs: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.