perl +如何声明数组
以下脚本(test.pl)在myfile.txt文件的$ first_line [1]和$ second_line [1]之间追加$ insert [1]文本并将输出发送到output.txtperl +如何声明数组
但如果我声明数组作为
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.
如何声明follwoing阵列?
备注:(没有我的阵列上的脚本做工精细)
利迪娅
#!/usr/bin/perl
# Slurp file myfile.txt into a single string
open(FILE,"myfile.txt") || die "Can't open file: $!";
undef $/;
my $file = <FILE>;
# Set strings to find and insert
my $count=1;
my $first_line[1]=")";
my $second_line[1]="NIC Hr_Nic (";
my $insert[1]="hello world
line 2
line 3 " ;
对于复合类型,如数组和哈希,你只需要声明的复合物作为一个词法变量:
my @first_line = ...;
从那里,你不需要申报复合元素,就像你在做什么。
您可能会从一本书开始,如学习Perl以获取该语言的基础知识。前面的一点研究可以为你节省很多痛苦和痛苦。
它看起来像你正试图影响文件的第一个几行。在这种情况下,一看便知,以How do I change, delete, or insert a line in a file, or append to the beginning of a file?:
如何更改,删除,或在一个文件中插入一行,或附加到文件的开始?
(贡献的布赖恩·d FOY)
从一个文本文件中插入,更改或删除线的基本思想包括阅读和打印文件要进行修改的位置上,使得变化,然后阅读并打印文件的其余部分。 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 $_;
}
接近$出; 在该基本表单中,添加您需要插入,更改或删除行的部分。
要在行首添加行,请在输入打印现有行的循环之前打印这些行。
open my $in, '<', $file or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";
print $out "# Add this line to the top\n"; # <--- HERE'S THE MAGIC
while(<$in>)
{
print $out $_;
}
close $ out; 要更改现有行,请插入代码以修改while循环内的行。在这种情况下,代码将查找所有小写版本的“perl”并将它们大写。每一行都会发生,所以一定要在每一行都做到这一点!
open my $in, '<', $file or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";
print $out "# Add this line to the top\n";
while(<$in>)
{
s/\b(perl)\b/Perl/g;
print $out $_;
}
close $ out; 要仅更改特定行,输入行号$。是有用的。首先阅读并打印您想要更改的行。接下来,阅读您想要更改的单行,更改并打印它。之后,阅读其余的行并打印这些行:
while(<$in>) # print the lines before the change
{
print $out $_;
last if $. == 4; # line number before change
}
my $line = <$in>;
$line =~ s/\b(perl)\b/Perl/g;
print $out $line;
while(<$in>) # print the rest of the lines
{
print $out $_;
}
要跳过行,请使用循环控制。本例中的下一个跳过注释行,并且一旦遇到END或DATA,最后一次停止所有处理。
while(<$in>)
{
next if /^\s+#/; # skip comment lines
last if /^__(END|DATA)__$/; # stop at end of code marker
print $out $_;
}
做同样的事情来删除一个特定的行,使用next来跳过你不想显示在输出中的行。只要
while(<$in>)
{
next unless $. % 5;
print $out $_;
}
如果出于某种奇怪的原因,你真的想看到整个文件一次,而不是加工生产线,由线,您可以在思乐普它(如你:这个例如每第五行跳过可以适合整个内存!):
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;
诸如File :: Slurp和Tie :: File之类的模块也可以提供帮助。但是,如果可以,请避免一次读取整个文件。在这个过程完成之前,Perl不会将该内存返回给操作系统。
您还可以使用Perl one-liners来就地修改文件。以下内容将inFile.txt中的所有'Fred'更改为'Barney',并用新内容覆盖文件。使用-p开关,Perl将用-e指定的代码包装一个while循环,并且-i打开就地编辑。当前行在$ 。使用-p,Perl会在循环结束时自动打印$的值。有关更多详细信息,请参阅perlrun。
perl -pi -e 's/Fred/Barney/' inFile.txt
为了inFile.txt的备份,给-ia文件扩展名补充:
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
通过使用['Tie :: File'](http://p3rl.org/Tie::File),您可以节省很多麻烦。您可以将文件内容视为数组。 – daxim 2010-08-15 08:25:59
你能给我wxample如何在脚本中使用它? – lidia 2010-08-15 08:31:00
我已经给了你一个链接到文档。阅读它。 – daxim 2010-08-15 08:32:24