Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Deleting a row in a file

by suneel.reddy (Novice)
on May 21, 2012 at 14:29 UTC ( #971622=perlquestion: print w/replies, xml ) Need Help??
suneel.reddy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I need to Open a file - Read - apply logic and the delete the records of that file which doesn't satisfy my logic. I don't need one more file to be created with the matching records( b'coz this file is already output of the earlier logic ).

Can someone help me in solving this ? Is there any way to remove a record from that file same as we have SPLICE to manipulate arrays ?



Replies are listed 'Best First'.
Re: Deleting a row in a file
by Anonymous Monk on May 21, 2012 at 14:33 UTC
Re: Deleting a row in a file
by ansh batra (Friar) on May 21, 2012 at 15:50 UTC
    Open the file in read mode ,keep it in @arr. Close the file handler. Open it again in write mode. Iterate @arr and apply logic Write it to file Close the file handler
Re: Deleting a row in a file
by zeni (Beadle) on May 22, 2012 at 06:29 UTC

    You will have to use 'tell' and 'seek'.

    'tell' gives the current cursor position and 'seek' can be used to set the filehandle position.

      No, this is a wrong idea.

      seek() and tell() normally have no role at all when processing a text file. That is because these functions work on byte offsets, not line offsets. And text lines usually have a variable number of bytes per line.

      In general it is not possible to "seek to line 4" because we don't know the number of bytes from the beginning of the file that line 4 starts at. In addition, if we could seek to "line 4", there is no way to edit that line to be either smaller or bigger.

      A disk file is like an old fashioned cassette tape. It is a stream of bits. There is no way to either: insert extra bits or delete bits from this stream of bits. With few exceptions, a disk file is a "read only" string of bits.

      To 'take out a song' or 'replace a song' on a cassette tape, you have to make a new cassette and copy the songs from the 'old cassette' to the "new cassette' and make adjustments during the copy. So it is with disk files.

      -Open the read_file_name
      -Open a temp_file for writing
      -Write to temp_file by reading "read_file name" and doing some edits(copy,delete(don't copy),modify).
      -close the read_file_name
      -rename the read_file_name as a .BAK file
      -close temp_file_name
      -rename temp_file_name to the original name (read_file)

      Part of the idea is to maintain at all times a "way back", even if the program crashes.

      I finished (I hope) a fight with MS update that required at the end of the day, 3 re-boots of my machine. This thing insisted upon re-installing and re-booting the same thing 3 times!

      My point is that even if your program works correctly, it could happen that it is terminated for reasons outside of your control. Make it as low a probability as you can that a file "gets lost".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://971622]
Approved by Jaap
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2018-01-23 22:54 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (254 votes). Check out past polls.