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.
In reply to Re^3: what does IO::File=GLOB mean?
by pjotrik
in thread what does IO::File=GLOB mean?
by ITmajor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |