ademmler has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on How to delete the line I have just read?

Replies are listed 'Best First'.
Re: How to delete the line I have just read?
by marto (Cardinal) on Aug 04, 2008 at 12:19 UTC
Re: How to delete the line I have just read?
by dHarry (Abbot) on Aug 04, 2008 at 12:20 UTC

    Type "delete a line from a file" in the search box above and click the search button my son.

    And 50 hail Mary's because you didn't put any effort in solving the problem yourself.

Re: How to delete the line I have just read?
by repellent (Priest) on Aug 04, 2008 at 20:41 UTC
    More details on what seems to be a trivial operation: http://perl.plover.com/yak/lightweight-db/

    You can get fancy with core module Tie::File

    Or, you can "reverse" your method and have your script print out the remaining files when it encounters the first failure.
      Hi to all, thanks for your answers. While you have been answering I was on the search for a solution by myself and I have found one. The major part for me is the change should be saved imedeatly to the file - after deleteing the line, because I need to have the actual status saved in the file even if the script crashes. And I did not want to ope/close/reoepn the file while each loop. I have found also Tie::File and with a litle trick it does exactly what I want.

      #!/usr/bin/perl
      use strict;
      use Tie::File;

      tie my @list, 'Tie::File', $list or die "Can't open list of files as array\n";

      my $records = @list;
      print"## Start: Found $records records in $list ##\n";

      my ($count, $counter);

      for ($count = 0; $count < $records; $count++ ) {
      #The important part ist to keep [0] here because it uses always the first line
      my $file = $list[0];
      #do some processing with the file from record. . .
      #delete line
      shift(@list) or die "Can't delete record $file in $list\n";
      }
      Regards Alex
Re: How to delete the line I have just read?
by l.frankline (Hermit) on Aug 04, 2008 at 14:05 UTC

    Hi,

    If I am not wrong then follow this code

    my $filename = 'yourfilename.txt'; open FILE, "$filename" || die "cant open file for read"; @data = <FILE>; close FILE; for my $line (0..$#data) { # execute process method -- do your operation stuffs here. &process($data[$line]); # if you want to delete, make the line with null value; $data[$line] = ""; } $entirefile = join "\n", @data; open OUT, "outfile.txt" || die "cant open file for write"; print OUT $entirefile; close OUT;

      That code could be prettier, but it has a serious bug in it:

      # if you want to delete, make the line with null value; $data[$line] = "";

      This doesn't actually delete the line; it replaces it with an empty line. Oh, and there's another bug -- why join lines on newlines when you haven't chomped away existing newlines? Here's much better code:

      my $filename = 'yourfilename.txt'; my $newfile = 'new_filename.txt'; filter_file( $filename, $newfile ); # this line may be unnecessary depending on your filesystem unlink $filename; rename( $filename, $newfile ); sub filter_file { my ($from, $to) = @_; open my $fh, $from or die "Can't read '$from': $!\n"; open my $out_fh, '>', $to or die "Can't write '$to': $!\n"; while (<$fh>) { next if should_delete_line($_); print {$out_fh}; } }