2010-08-15 88 views
-2

以下腳本(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 " ; 
+0

通過使用['Tie :: File'](http://p3rl.org/Tie::File),您可以節省很多麻煩。您可以將文件內容視爲數組。 – daxim 2010-08-15 08:25:59

+0

你能給我wxample如何在腳本中使用它? – lidia 2010-08-15 08:31:00

+2

我已經給了你一個鏈接到文檔。閱讀它。 – daxim 2010-08-15 08:32:24

回答

1

您應該使用my @first_line =();宣佈一個新的空數組。您不必提供尺寸。

但是,您發佈的代碼存在許多錯誤。例如,如果你只有一個元素,爲什麼要使用一個數組呢?

+0

其唯一的例子(我使用多於一個元素)請告訴我,如果U看到另一個錯誤? – lidia 2010-08-15 08:20:10

+0

您是否看到代碼中存在其他衝突? – lidia 2010-08-15 08:22:37

+4

根本沒有必要給數組賦值'()。這只是噪音。 – bart 2010-08-15 09:49:33

2

對於複合類型,如數組和哈希,你只需要聲明的複合物作爲一個詞法變量:

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 $_; 
    } 

要跳過行,請使用循環控制。本例中的下一個跳過註釋行,並且一旦遇到ENDDATA,最後一次停止所有處理。

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 
+0

嗨布賴恩 如果你可以請告訴我,如果你在我的代碼中看到一些其他衝突 THX – lidia 2010-08-15 08:53:58

+0

@lidia:你的代碼存在很多問題,但是由於你堅持不聽你在這個問題上給你的建議,因爲以及你最近的其他問題,那麼沒有什麼人可以做。 *閱讀一本書*和*學習你正在嘗試使用的語言。 – Ether 2010-08-16 01:51:51