John W. Krahn wrote:

As you have seen when using Tie::File removing the contents of a line do not
remove that line.  You have to splice the tied array:

my $line_to_remove;

for my $i ( 0 .. $#file ) {
  $line_to_remove = $i if /foo/;
}

splice @file, $line_to_remove, 1;

If you have more than one line to remove:

my @lines_to_remove;

for my $i ( 0 .. $#file ) {
  unshift @lines_to_remove, $i if /foo/;
}

  for my $i ( 0 .. $#file ) {
    unshift @lines_to_remove, $i if $file[$i] =~ /foo/;
  }

OR

  my $i = 0;
  foreach ( @file ) {
    unshift @lines_to_remove, $i if /foo/;
    ++$i;
  }

for ( @lines_to_remove ) {
  splice @file, $_, 1;
}


And if you want to do it with one loop:


for my $i ( reverse 0 .. $#file ) {
  splice @file, $i, 1 if /foo/;
}

Nice use of unshift() to avoid reverse(). Like it.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to