Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

RE: Writing to files

by Anonymous Monk
on Apr 08, 2000 at 07:03 UTC ( #7175=note: print w/replies, xml ) Need Help??


in reply to Writing to files

I've got a program online that does insertion into a file (http://www.pobox.com/~japhy/tmp/perl/add_lines). But the bigger question is, WHY DO YOU NEED TO DO THIS? Observe:
open FILE, ">>data" or die "can't append to data: $!"; print FILE "NEW RECORD\n"; close FILE; # now, to store the file's contents in an array, MOST # RECENT DATA FIRST, use unshift instead of push! open FILE, "data" or die "you know the drill... $!"; unshift @contents, $_ while <FILE>; close FILE;

Replies are listed 'Best First'.
RE: RE: Writing to files
by turnstep (Parson) on Apr 08, 2000 at 07:23 UTC
    Why do you need to do what? Your question is not clear. Yes, you can unshift into an array, but that has nothing to do with writing the file...
      Not to put words in his mouth, but the point I think japhy (right?) was trying to make was that, if the reason you're writing to the beginning of the file is that you think that there's where you should store the most "recent" information, then there's really no point.

      Just keep your file in chronological order (oldest at beginning, newest at end) and, when you're reading the file into memory, use unshift rather than push so that the newest data ends up at the beginning of the array.

      Which definitely makes sense, and makes the problem quite a lot less complicated, if that's the reason the op wanted to write to the beginning of the file.

        Yeah, that was me. And you're correct -- there's no need to write to the TOP of a file, just so that it is in newest-to-oldest order. Use the append + unshift method. It's smarter.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://7175]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-08-08 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?