[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Removing an element from an array



What is the best way to remove an element from the middle of an array?

For example, if I have an array containing the ten lines of a text file,
one line in each element of the array, and I want to delete line $x from
the file, what is the best wau to do it?

I'm trying:

      open(FILE, "$file")|| die "could not open $file $!";
      @lines = <FILE>;
      close FILE;
      $no_of_lines=@lines;

      # copy each element from @lines[0], stopping before @lines[$x]
      for ($k=0; $k<$x; $k++)
      {
         @new_lines[$k] = @lines[$k];
      }

      # copy the remaining elements from @lines[$x+1]...
      for ($k=($x+1); $k<$no_of_lines; $k++)
      {
         @new_lines[$k-1] = @lines[$k];
      }

      open(FILE, ">$file")|| die "could not open $file $!";
      foreach $line (@new_lines)
      {
         print FILE "$line\n";
      }
      close FILE;

However, it doesn't seem to be working...

Is there a better way to do this?

Thanks,

Jake



# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org