Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > reading file round and round

Reply
Thread Tools

reading file round and round

 
 
cerr
Guest
Posts: n/a
 
      03-19-2010
Hi There,

I read out the content from a file like:
foreach $line (<$handle>) {
print $line;
sleep(1);
}
whixh works well so far. But what I would like is, if the loop gets to
eof, it should start over on top again. How can i reset the reading
pointer back to the beginning of the file?

Thanks,
--
roN
 
Reply With Quote
 
 
 
 
cerr
Guest
Posts: n/a
 
      03-19-2010
On Mar 19, 11:43*am, cerr <(E-Mail Removed)> wrote:
> Hi There,
>
> I read out the content from a file like:
> foreach $line (<$handle>) {
> * * * * * print $line;
> * * * * * sleep(1);
> * * * * }
> whixh works well so far. But what I would like is, if the loop gets to
> eof, it should start over on top again. How can i reset the reading
> pointer back to the beginning of the file?


Do I actually need to close() and reopen my file or is there another
way to achieve this?

>
> Thanks,
> --
> roN


 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      03-19-2010
On Mar 19, 11:46*am, cerr <(E-Mail Removed)> wrote:
> On Mar 19, 11:43*am, cerr <(E-Mail Removed)> wrote:
>
> > Hi There,

>
> > I read out the content from a file like:
> > foreach $line (<$handle>) {
> > * * * * * print $line;
> > * * * * * sleep(1);
> > * * * * }
> > whixh works well so far. But what I would like is, if the loop gets to
> > eof, it should start over on top again. How can i reset the reading
> > pointer back to the beginning of the file?

>
> Do I actually need to close() and reopen my file or is there another
> way to achieve this?
>
>


LOOP:
{
foreach $line (<$handle>)
{
...
}
seek($handle, 0, 0) or die ...
redo LOOP;
}

But if you're actually just trying to emulate a
tail -f, look at the seek doc (perldoc -f seek)
for details on the WHENCE argument. You could
pick up newly appended lines and avoid reading through the entire file
with each loop.

--
Charles DeRykus
 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      03-19-2010
On Mar 19, 12:43*pm, cerr <(E-Mail Removed)> wrote:
> Hi There,
>
> I read out the content from a file like:
> foreach $line (<$handle>) {
> * * * * * print $line;
> * * * * * sleep(1);
> * * * * }
> whixh works well so far. But what I would like is, if the loop gets to
> eof, it should start over on top again. How can i reset the reading
> pointer back to the beginning of the file?



If an infinite loop is what you want, you can always use the
Tie::File module (which you may already have in your installation of
Perl) and just increment the index variable, making sure to "mod" it
by the number of lines.

Here's an example:


#!/usr/bin/perl

use strict;
use warnings;

my $fileName = 'file.txt';

use Tie::File;
tie my @lines, 'Tie::File', $fileName
or die "Could not open file '$fileName': $!\n";

die "No lines found in '$fileName'.\n" unless @lines;

my $lineNum = 0;

while (1)
{
print $lines[$lineNum], "\n";
sleep(1);
}
continue
{
# Increment $lineNum, but make sure it doesn't exceed $#lines:
$lineNum++;
$lineNum %= @lines;
}

__END__


This solution may seem overkill for your example, but if you have a
more complex task where you'd rather traverse arrays instead of
manipulating file handles, Tie::File is quite nice.

Cheers,

-- Jean-Luc
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-19-2010
cerr <(E-Mail Removed)> wrote:
>Hi There,
>
>I read out the content from a file like:
>foreach $line (<$handle>) {
> print $line;
> sleep(1);
> }
>whixh works well so far. But what I would like is, if the loop gets to
>eof, it should start over on top again. How can i reset the reading
>pointer back to the beginning of the file?


perldoc -f seek

jue
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-19-2010
"C.DeRykus" <(E-Mail Removed)> wrote:
>On Mar 19, 11:46*am, cerr <(E-Mail Removed)> wrote:
>> On Mar 19, 11:43*am, cerr <(E-Mail Removed)> wrote:
>>
>> > I read out the content from a file like:
>> > foreach $line (<$handle>) {
>> > * * * * * print $line;
>> > * * * * * sleep(1);
>> > * * * * }
>> > whixh works well so far. But what I would like is, if the loop gets to
>> > eof, it should start over on top again. How can i reset the reading
>> > pointer back to the beginning of the file?

>>
>> Do I actually need to close() and reopen my file or is there another
>> way to achieve this?
>>
>>

>
>LOOP:
>{
> foreach $line (<$handle>)
> {
> ...
> }
> seek($handle, 0, 0) or die ...
> redo LOOP;
>}


Why not

while(1){
foreach $line (<$handle>){
{ ...
}
seek(...) or die ...
}

instead of that ugly label?

jue
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-20-2010
On 2010-03-19 19:39, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth "C.DeRykus" <(E-Mail Removed)>:
>> seek($handle, 0, 0) or die ...

>
> Don't do that (I know perldoc -q tail recommends it, but it ought to be
> updated). Use the constants from the Fcntl module, they're more
> portable.


Are they really?

hp
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
So, round and round... 70-682... MSLearning, please read! DaGenester MCITP 2 05-20-2010 04:36 PM
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
VS2005 - Going round and round in circles again :) postings@alexshirley.com ASP .Net 0 06-22-2006 11:26 AM
Float.round - should it be round-to-even OliverMarchand Ruby 2 04-12-2006 12:06 PM
Computer Goes Round and Round Checking Alan Computer Support 6 10-08-2004 08:24 AM



Advertisments