?
Solved

Writing new flat file data

Posted on 2003-02-22
7
Medium Priority
?
209 Views
Last Modified: 2010-03-05
Is there a way to write new data into a flatfile putting the new data ABOVE the old data while still keeping the old data?
The way I have my script set up is, it reads in the top line of the flatfile, then displays that in a form.  Then writes the new data into the flatfile, currently it is writing to the bottom of the file and I need the most recent data to post on the top of the flatfile.

sub OPEN_PO_FILE_EDIT_MAIN{
$pdir = '../customers/lay/';
$pdata = $po;
$pext = '.po';
open(pFILE, ">>$pdir$pdata$pext");
flock(pFILE, 2);
print pFILE "$po||$layawaynum||$name||$origbalance||$prevbalance||$paymentamount||$newbalance||$mop||$odate||$date \n";
flock(pFILE, 8);
close(pFILE);
}

if viewing the flatfile raw it would look like:
$most recent||$layawaynum||$name||$origbalance||$prevbalance||$paymentamount||$newbalance||$mop||$odate||$date
$old||$layawaynum||$name||$origbalance||$prevbalance||$paymentamount||$newbalance||$mop||$odate||$date

Thanks for reading:
DaVe
0
Comment
Question by:nyxoh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:herrmann1001
ID: 8007038
You can always read the whole file into memory, and sort it to the most recent order, and write it back to the file.

I dont know which platform you are on, but there are usually a sort command available from the commandoline.

cat appended_flatfile | sort > sorted_flatfile

and if you want it to be done in the perl script..

$sort_result = `cat appended_flatfile | sort > sorted_flatfile`


best of times!
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 120 total points
ID: 8009860
Tie::File should let you accomplish exactly what you want.  It allows you to access the file as an array, so you could just do:

sub OPEN_PO_FILE_EDIT_MAIN{
$pdir = '../customers/lay/';
$pdata = $po;
$pext = '.po';
tie @fil, 'Tie::File', "$pdir$pdata$pext"
    or die "could not tie $pdir$pdata$pext: $!";
unshift @fil, "$po||$layawaynum||$name||$origbalance||$prevbalance||$paymentamount||$newbalance||$mop||$odate||$date \n";
untie @fil;
}
0
 
LVL 2

Expert Comment

by:Itatsumaki
ID: 8010312
The other solution not mentioned would be to just write everything to a temp-file.

So:
1. Open a temp-file for input (OUT)
2. Print OUT new stuff
3. Print OUT old stuff
4. Close temp-file (OUT)
5. unlink(flat_file)
6. open(flat_file, IN)
7. open(temp_file, OUT)
8. while (<IN>) { print OUT }

That's slower on the disk usage, but is much less memory intensive because it doesn't try to slurp the file all at once.  It's also (to me, as a simple-minded programmer) somewhat more intuitive.

Hope this helps!
Tats
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:wilcoxon
ID: 8010497
Tie::File does not read the whole file into memory.  You can set the maximum amount of memory (2MB default) to use for the read cache and deferred write buffer (2MB default - must be <= total memory).

The smaller the read cache, the slower it will run if you are reading/writing alot lines/changes (must re-read/re-write from/to disk each time if the line isn't in the cache).  It shouldn't affect pre-pending lines to the file much either way.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8010508
It also looks like Tie::File was put into the core with Perl 5.8.0.
0
 
LVL 2

Expert Comment

by:Itatsumaki
ID: 8011762
Yup Wilcoxon is right about Tie::File. I should think before I write next time. :)

Tats
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8012020
Don't worry about it.  If you read the "encrypting passwords using perl md5 algorithm" thread, you noticed I posted multiple times in that thread without fully thinking before hitting submit.  <sheepish grin>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question