2010-06-14 31 views
1

如何更改此代码插入行如果丢失而不删除现有的?如何在使用Tie :: File时有条件地插入行?

tie my @lines, 'Tie::File', $fn or die "could not tie file: $!";   

for (my $i = 0; $i < @lines; $i++) { 

    if ($ln_title == 0) {        

     if ($i < $#lines and $lines[$i] =~ /(\s+TRACK \d\d .*)$/) {   

     $lines[$i+1] = ' TITLE ""'; 
     } 
    } 
}      

untie @lines; 

回答

4

您的要求看起来有点模糊,所以我很难告诉你想要什么。

如果你想一个TRACK线后立即插入TITLE ""线,而无需更换(重写),其为TRACK线后最初的线,那么你可以使用下面在$lines[$i+1] = …的地方:

splice @lines, $i+1, 0, ' TITLE ""'; 
+0

是。这正是我所期待的。 非常感谢。 – thebourneid 2010-06-14 04:18:25