我需要編輯的格式類似的文本文件:的Perl:號碼在文本文件中的每一個字的情況下
> Word text text text text
Text
Text
> Word text text text text
Text
Text
,使它看起來像:
>Word1 text text text text
Text
Text
>Word2 text text text text
Text
Text
基本上,我需要修改字符串「Word」的每個實例,並將其轉換爲「Word」,後面跟着一個與該字符串出現在文本文件中的實例數相對應的數字。我是Perl新手,不知道自己在做什麼。下面是我有:
$file = "test.txt";
my %count;
my $word = " Word";
#Open and read data
open (FILE, "<$file") or die "Cannot open $file: $!\n";
@lines= <FILE>;
foreach my $word{
my $count++;
}
close FILE;
my $counter = my $count + 1;
my $curr_ct = my $counter - my $count;
#Open same file for editing now
open (STDOUT, ">$file") or die "Cannot open $file: $!\n";
while (my $count > 0){
s/ Word/Word$curr_ct/
my $count--;
my $curr_ct = my $counter - my $count;
print;
}
close STDOUT;
'my'創建一個新變量,所以'my $ count ++;','my $ count + 1','my $ counter - my $ count'和'my $ count - '''沒有意義。 – ikegami
'foreach my $ word {my $ count ++; }缺少循環的列表! – ikegami
'打開文件,...'應該是'打開我的$ FILE,...'。不要使用全局變量。 – ikegami