perl+;如何声明数组

perl+;如何声明数组,perl,Perl,下面的脚本(test.pl)在myfile.txt文件的$first_行[1]和$second_行[1]之间追加$insert[1]文本,并将输出发送到output.txt 但是如果我将数组声明为 my $first_line[1]=")"; my $second_line[1]="NIC Hr_Nic ("; my $insert[1]="hello world line 2 line3 " 我明白了 syntax

下面的脚本(test.pl)在myfile.txt文件的$first_行[1]和$second_行[1]之间追加$insert[1]文本,并将输出发送到output.txt

但是如果我将数组声明为

 my $first_line[1]=")"; 
 my $second_line[1]="NIC Hr_Nic ("; 
 my $insert[1]="hello world
                line 2
                line3 "
我明白了

 syntax error at ./test.pl line 10, near "$first_line["
 syntax error at ./test.pl line 11, near "$second_line["
 syntax error at ./test.pl line 12, near "$insert["
 Execution of ./test.pl aborted due to compilation errors.
如何声明以下数组

备注:(没有阵列上的my,脚本工作正常)

利迪亚

#/usr/bin/perl
#将文件myfile.txt压缩成单个字符串
打开(文件,“myfile.txt”)| | die“无法打开文件:$!”;
未定义$/;
我的$file=;
#设置要查找和插入的字符串
我的$count=1;
我的$first_行[1]=”;
我的$second_行[1]=“NIC Hr_NIC(”;
我的$insert[1]=“你好,世界”
第2行
第3行”;

您应该使用
my@first_line=();
来声明新的空数组。您不必给出大小


但是,您发布的代码有很多错误。例如,如果您只有一个元素,为什么要使用数组呢?

对于数组和哈希等复合类型,您只需要将复合声明为词法变量:

 my @first_line = ...;
从这里开始,您不需要像现在这样声明复合元素

你可以从一本书开始,比如学习Perl来学习该语言的基础知识

看起来您试图影响文件的前几行。在这种情况下,请参阅以下内容的答案:


如何在文件中更改、删除或插入行,或追加到文件开头

(布莱恩·福伊撰稿)

插入、更改或删除文本文件中的一行的基本思想包括读取并打印文件到要更改的位置,进行更改,然后读取并打印文件的其余部分。Perl不提供对行的随机访问(特别是因为记录输入分隔符$/,是可变的),尽管像Tie::File这样的模块可以伪造它

执行这些任务的Perl程序的基本形式是打开文件,打印其行,然后关闭文件:

open my $in,  '<',  $file      or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";

while( <$in> )
    {
    print $out $_;
    }
要备份infle.txt,请为-i提供要添加的文件扩展名:

perl -pi.bak -e 's/Fred/Barney/' inFile.txt
要仅更改第五行,可以添加测试检查$,输入行号,然后仅在测试通过时执行操作:

perl -pi -e 's/Fred/Barney/ if $. == 5' inFile.txt
要在某一行之前添加行,可以在Perl打印$之前添加一行(或多行!):

perl -pi -e 'print "Put before third line\n" if $. == 3' inFile.txt
您甚至可以在文件的开头添加一行,因为当前行打印在循环的末尾:

perl -pi -e 'print "Put before first line\n" if $. == 1' inFile.txt
要在文件中已经存在的一行之后插入一行,请使用-n开关。它与-p类似,只是它不会在循环结束时打印$,因此您必须自己执行。在这种情况下,请先打印$,然后打印要添加的行

perl -ni -e 'print; print "Put after fifth line\n" if $. == 5' inFile.txt
要删除行,请仅打印所需的行

perl -ni -e 'print unless /d/' inFile.txt

    ... or ...

perl -pi -e 'next unless /d/' inFile.txt

这是唯一的例子(我使用了不止一个元素),请告诉我是否看到其他错误?根本不需要分配
()
到数组。这只是噪音。使用可以避免很多麻烦;您可以将文件内容视为数组。您能给我一个在我的脚本中如何使用的示例吗?我已经给了您一个文档链接。请阅读。我为什么需要使用此模块?为什么从数组索引1开始?数组从0开始。您好,布莱恩,如果您能告诉我的话如果在我的代码中看到其他冲突,请与我联系THX@lidia当前位置您的代码有很多问题,但由于您坚持不听取在这个问题上以及最近其他问题上给您的建议,因此任何人都无能为力。阅读一本书,学习您尝试使用的语言。
open my $in,  '<',  $file      or die "Can't read old file: $!"
open my $out, '>', "$file.new" or die "Can't write new file: $!";

my @lines = do { local $/; <$in> }; # slurp!

    # do your magic here

print $out @lines;
perl -pi -e 's/Fred/Barney/' inFile.txt
perl -pi.bak -e 's/Fred/Barney/' inFile.txt
perl -pi -e 's/Fred/Barney/ if $. == 5' inFile.txt
perl -pi -e 'print "Put before third line\n" if $. == 3' inFile.txt
perl -pi -e 'print "Put before first line\n" if $. == 1' inFile.txt
perl -ni -e 'print; print "Put after fifth line\n" if $. == 5' inFile.txt
perl -ni -e 'print unless /d/' inFile.txt

    ... or ...

perl -pi -e 'next unless /d/' inFile.txt