Modifying the input

T

Tan Rezaei

Hi All,

I know this is proabably simple but I can't get my mind around it:
I need to read from a file that has many records.
I need to read from this file and depending on what the records say I
need to perform some certain tesks. Well I got that all figured out.

My problem is that I need to change one of the fields on the records
showing that I have already read it and setting some status code so
next time I read the file I will know if I need to do anything more
with that record.

So how do I change records in the file I am reading from? In some
instances I may even need to removed the record from the middle of the
file.

I know you're thinking that I should be using a database but take my
word that is not going to happen in the environment I am working in.
It HAS TO BE A FLAT FILE. I would love to use a DB and import the
files into it and work with them but my hands are tied.

Thanks

T
 
T

Tore Aursand

So how do I change records in the file I am reading from? In some
instances I may even need to removed the record from the middle of the
file.

Write to a new, temporary file, while reading through the original file.
Change whatever suits you on the way, and finally overwrite [1] the
original file with the new (temporary) file.

[1] perldoc -f rename
perldoc -f unlink
perldoc File::Copy
I know you're thinking that I should be using a database but take my
word that is not going to happen in the environment I am working in.
It HAS TO BE A FLAT FILE. I would love to use a DB and import the
files into it and work with them but my hands are tied.

Take a look at CPAN [2]; There are lots of modules there which lets you
work with flat files as you'd do with a database.

[2] <http://www.cpan.org/>
 
D

David K. Wall

Tore Aursand said:
So how do I change records in the file I am reading from? In some
instances I may even need to removed the record from the middle
of the file.

Write to a new, temporary file, while reading through the original
file. Change whatever suits you on the way, and finally overwrite
[1] the original file with the new (temporary) file.

Or just use Tie::File, which makes it much easier, IMHO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top