I hope this helps explain what I am trying to do...
My first foreach ($line) collects a row of data from a file. The second foreach ($line) reformats the first "cell" (for lack of a better word) into a new date/time format.
By using the seek function I was attempting to append to the end of the file instead of overwriting it. What is the correct way to do this?
Included in my code I have this line:
$fh = IO::File->new_tmpfile or die "Unable to make new temp file: $!";
Does this put the contents of the file in $fh or is it just pointing to the file? Because this is a randomly generated file, I want to be able to access the file using a variable since I don't know the name of the temp file.
How can I do this and be able to display what is in the file? | [reply] [d/l] [select] |
I'm not sure why you're using a file for this. I would store the values in an array, if there's not a huge amount of them (which is not the case, since you already have the whole @data in memory). But perhaps there's a reason, not shown in the snippet you've posted.
Your first foreach $line (@data) processes the data line by line. Your foreach ($line) is nested inside the first foreach and iterates through the list consisting of one item: ($line). I.e. it has no effect - it is the same as if the inner loop wasn't there.
Your seek is again inside the foreach $line (@data), therefore you move your position to the beginning of the file before each write. new_tmpfile opens a new file for read/write. The position is already on the start of the file, you don't have to seek before writing.
When you're done writing and want to read from the file, you'll have to seek to the beginning of the file (your arguments to seek are fine for this purpose). Then you may start writing like from any other filehandle (while (<$fh>) {... or simply print <$fh>;). Don't forget to flush your file when you're finished writing.
And another remark, you don't have to define variables for parts of the split you don't need, you may just call ($timestamp, undef, undef, $out,...) = split(/\t/, $line);
HTH
UPDATE: Here's a code sample. Looks like the file is flushed because of the seeking, so it's not necessary to call flush.
#!/usr/bin/perl
use warnings;
use strict;
use IO::File;
my $fh = IO::File->new_tmpfile or die "Unable to make new temp file: $
+!";
my @data = <DATA>;
foreach my $line (@data) {
chomp $line;
my ($first, undef, undef, $last) = split(/\t/, $line);
print $fh "$first $last\n";
}
#$fh->flush();
$fh->seek(0, 0) or die "Seek: $!";
print <$fh>;
$fh->close();
__DATA__
1 2 3 4
5 6 7 8
9 0 1 2
Another UPDATE: Changed the seek to a more object-ish notation, simplified the print. | [reply] [d/l] [select] |
my @data = <DATA>;
foreach my $line (@data) {
| [reply] [d/l] |
The call to seek() with those parameters is not doing what you want, then. See seek (or try perldoc -f seek on your system) to see what the arguments for seek() are.
| [reply] [d/l] |