简体   繁体   English

为什么在此Perl代码中出现“'$ rocks ['附近的语法错误””?

[英]Why do I get “Syntax error near '$rocks['” in this Perl code?

When I run this program on ActivePerl 5.8 on Windows XP, I get a syntax error: 在Windows XP的ActivePerl 5.8上运行此程序时,出现语法错误:

#!C:\Perl\bin\perl.exe

use strict; # enabled
use warnings;


(my $rocks[0], my $rocks[1]) = qw/Hello World/; # Syntax error near '$rocks['

my $rocks[2] = 'Tom'; # Syntax error near '$rocks['
my $rocks[3] = 'Cat'; # Syntax error near '$rocks['

print $rocks[0];
print $rocks[1];
print $rocks[2];
print $rocks[3];

When I used ( @ ) before the name of the array rocks , it worked well. 当我在数组rocks的名称前使用( @ )时,它运行良好。 How do I fix the error above when I used $ ? 使用$时,如何解决以上错误? Thank you. 谢谢。

my @rocks = qw{Hello World Tom Cat}; # worked well.

Don't use my again and again to declare $rocks[0] , $rocks[1] etc. Declare the array once ( @rocks ) and use it. 不要一次又一次地使用my声明$rocks[0]$rocks[1]等。一次声明数组( @rocks )并使用它。

The corrected code is something like this: 更正后的代码是这样的:

use strict;
use warnings; 
my @rocks; ## declare the array here

($rocks[0], $rocks[1]) = qw/Hello World/; 
$rocks[2] = 'Tom'; 
$rocks[3] = 'Cat';

Use the push operator: 使用push操作符:

my @rocks;

push @rocks, qw/ Hello World /;
push @rocks, "Tom";
push @rocks, "Cat";

Avoiding explicit and redundant array indices helps future-proof your code. 避免显式和冗余的数组索引可帮助您的代码适应未来需求。 For example, if you find you need to change your initialization, you can't botch an array index that isn't there. 例如,如果发现需要更改初始化,则无法破坏不存在的数组索引。

I think you need to declare my @rocks and then not use my any more when referring to $rocks[xxx] . 我认为您需要声明my @rocks ,然后在引用$rocks[xxx]时不再使用my

If you don't know how many elements are going to be in there, you can use push to add new elements into the (initially 0-sized) array. 如果您不知道其中有多少个元素,可以使用push将新元素添加到(最初为0大小)数组中。

You are redeclaring @rocks several times. 您多次声明@rocks Try something like this instead: 尝试这样的事情:

my @rocks;

$rocks[0] = 'Tom';
$rocks[1] = 'Cat';

etc. 等等

You can first declare the array at the top as: 您可以首先在顶部将数组声明为:

my @rocks;

And remove my declaration from all other places. 并从其他所有地方删除我的声明。

Your code becomes: 您的代码变为:

#!C:\Perl\bin\perl.exe
# ActivePerl 5.8 based
use strict; # enabled
use warnings;

my @rocks;

($rocks[0], $rocks[1]) = qw/Hello World/; # Syntax error near '$rocks['

$rocks[2] = 'Tom'; # Syntax error near '$rocks['
$rocks[3] = 'Cat'; # Syntax error near '$rocks['

print $rocks[0];
print $rocks[1];
print $rocks[2];
print $rocks[3];

Why don't you just put it straight into @rocks ? 为什么不直接将它放入@rocks呢?

use strict;
use warnings;

my @rocks = qw'Hello World';

my $rocks[2] = 'Tom';
my $rocks[3] = 'Cat';

print $rocks[0];
print $rocks[1];
print $rocks[2];
print $rocks[3];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我运行Perl单行代码时,为什么在意外的标记`(')附近出现“ -bash:语法错误”? - Why do I get “-bash: syntax error near unexpected token `('” when I run my Perl one-liner? 意外标记'|'附近的语法错误 Perl代码 - syntax error near unexpected token `|' Perl code “ =”附近的PERL语法错误 - PERL syntax error near “= ) ” 为什么我用此Perl代码得到错误500? - why do i get error 500 with this perl code? 为什么在我的代码生成程序中出现此语法错误? - Why do I get this syntax error in my code generating program? 在Perl中,为什么在尝试使用字符串eval时会出现语法错误? - In Perl, why do I get a syntax error when I try to use string eval? Perl:为什么会出现错误“文件名,目录名或卷标签语法不正确。” - Perl: Why do I get error “The file name, directory name, or volume label syntax is incorrect.” 为什么使用 Perl Here-Document 时会出现语法错误? - Why do I get syntax errors with Perl Here-Document? 当我运行一个以“#!/ usr / bin / perl -w”开头的文件时,出现错误:“第153行的语法错误,靠近”=〜?“” - When I run a file which is begin with “#!/usr/bin/perl -w”, I get a error: “syntax error at line 153, near ”=~ ?“” 当我尝试创建具有外键约束的SQLite表时,为什么会出现“ DBD :: SQLite :: db失败:靠近“事务”:语法错误”的问题? - Why do I get 'DBD::SQLite::db do failed: near “transaction”: syntax error' when I try to create a SQLite table with a foreign key constraint?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM