tags:

views:

45

answers:

1

How can I change this code to insert lines if missing without deleting existing ones?

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  A: 

Your requirements seem a bit vague, so it is hard for me to tell what you want.

If you want to insert a TITLE "" line immediately after a TRACK line without replacing (overwriting) the line that was originally after the TRACK line, then you can use the following in the place of $lines[$i+1] = …:

splice @lines, $i+1, 0, '    TITLE ""';
Chris Johnsen
Yes. This is exactly what I was looking for.Many thanks.
thebourneid