Re: Time and again
- From: "Peter J. Holzer" <hjp-usenet2@xxxxxx>
- Date: Sat, 14 Jun 2008 08:40:34 +0200
On 2008-06-10 21:22, Jürgen Exner <jurgenex@xxxxxxxxxxx> wrote:
"Peter J. Holzer" <hjp-usenet2@xxxxxx> wrote:
On 2008-06-10 11:30, Jürgen Exner <jurgenex@xxxxxxxxxxx> wrote:
markdibley <markdibley@xxxxxxxxx> wrote:
I write that to a file and later I read it in
Why are you dumping the data into a file and then reading it back in?
That's what files are for. Why would you write something into a file if
you didn't plan to read it at some time?
Well, why would you write something to a file and then read it back into
the program if you already have the information in the program to begin
with?
Because you usually don't have it already in this process. Either
because it was written by a different process (that's the common case
for files - one process writes it, some other process reads it - maybe
only milliseconds later, maybe years later), or because your data
doesn't fit into memory all at once.
It all depends on what the OP meant with "later".
Is there some reason why you would think that the most stupid
possibility is the most likely?
I have seen code like
system ("myprog > temp.txt");
open (TMP, '<', 'temp.txt');
@result = <TMP>;
Here the programmer writes information to a file and later reads it back
into the program, but unless there are very special circumstances
probably nobody would call this good code.
However, the problem "how do I print a timestamp so that I can parse it
again" does not become easier if you replace the above code with
open(my $fh, '-|', 'myprog');
@result = <$fh>;
close($fh);
in both cases 'myprog' needs to write the timestamps in a defined
format, and your script needs to know how to parse that. Whether the
data between the two processes is exchanged via a temporary file or a
pipe (or a socket, or shared memory, or ...) is irrelevant to the
question.
A case where it was relevant would be something like this:
my $now = time();
print $fh localtime($now), "\n";
seek($fh, 0, 0);
$line = <$fh>;
$now = parse($line);
That's clearly stupid, but there is no reason to assume the OP would be
doing that, even if he wrote exactly that code into his demo script.
Because a demo script is there to illustrate the problem in the shortest
possible manner.
The OP already clarified that this is not what he was doing but that he
was dealing with log files. But that detail was missing in the original
post.
It was probably missing because the way he intended to use the file
(some process writes a file, some other process reads it) is the most
common case by far. Why should he mention that he wasn't going to do
something unlikely and stupid? Especially if that is completely
unrelated to the question?
hp
.
- Follow-Ups:
- Re: Time and again
- From: Jürgen Exner
- Re: Time and again
- References:
- Time and again
- From: markdibley
- Re: Time and again
- From: Jürgen Exner
- Re: Time and again
- From: Peter J. Holzer
- Re: Time and again
- From: Jürgen Exner
- Time and again
- Prev by Date: Re: discover invisible characters
- Next by Date: Re: Reading a line from a CVS file into an array.
- Previous by thread: Re: Time and again
- Next by thread: Re: Time and again
- Index(es):
Relevant Pages
|