reading file round and round

Discussion in 'Perl Misc' started by cerr, Mar 19, 2010.

  1. cerr

    cerr Guest

    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
     
    cerr, Mar 19, 2010
    #1
    1. Advertising

  2. cerr

    cerr Guest

    On Mar 19, 11:43 am, cerr <> 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
     
    cerr, Mar 19, 2010
    #2
    1. Advertising

  3. cerr

    C.DeRykus Guest

    On Mar 19, 11:46 am, cerr <> wrote:
    > On Mar 19, 11:43 am, cerr <> 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
     
    C.DeRykus, Mar 19, 2010
    #3
  4. cerr

    Guest

    On Mar 19, 12:43 pm, cerr <> 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
     
    , Mar 19, 2010
    #4
  5. cerr <> 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
     
    Jürgen Exner, Mar 19, 2010
    #5
  6. "C.DeRykus" <> wrote:
    >On Mar 19, 11:46 am, cerr <> wrote:
    >> On Mar 19, 11:43 am, cerr <> 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
     
    Jürgen Exner, Mar 19, 2010
    #6
  7. On 2010-03-19 19:39, Ben Morrow <> wrote:
    > Quoth "C.DeRykus" <>:
    >> 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
     
    Peter J. Holzer, Mar 20, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    679
    Kevin Spencer
    Nov 11, 2004
  2. Replies:
    0
    Views:
    1,097
  3. Replies:
    0
    Views:
    787
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    375
    Hrvoje Niksic
    May 25, 2007
  5. OliverMarchand

    Float.round - should it be round-to-even

    OliverMarchand, Apr 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    225
    OliverMarchand
    Apr 12, 2006
Loading...

Share This Page