Benjamin Jeeves wrote:
> On Sunday 09 Feb 2003 7:23 pm, Rob Dixon wrote:
>>
>> I'm pretty sure that, as I said before, you're reading from the file
>> in several different places. The 'start' subroutiine will read the
>> first
>> line and then call 'build_db' which may well read all of the rest of
>> the lines. If you call 'alert' then there will be no data remaining
>> to
>> be read and its while loop will exit immediately. If you don't call
>> 'build_db' then all lines in the file but the first one will still be
>> unread and there will be some work for 'alert' to do.
>>
>> I'm not at all sure what your program is supposed to be doing so I
>> can't suggest what may be the correct coding. Are you intending
>> that both 'build_db' and 'alert' read all the way through the file?
>
> Yes I would like build_db and alert to read all the way through the
> file can that be done if so what do you think is the best way or
> point me in the right way

Fine. I thought so.

Well, you had the right idea when you coded

    seek filein,0,1;

after the while loop in sub 'start'. However the '1' means to set the
file pointer relative to its current position, so with an offset of zero
the statement leaves things as the are. What you want is

    seek filein, 0, 0;

or, far better

    seek filein, 0, SEEK_SET;

for which you need to use the Fcntl module which defines the
constant SEEK_SET as zero, but leaves the code much more readable.
Furthermore, this needs to be before each of the loops in the
subroutines which read through the file.

Finally, I don't think you want to do any file reading in sub 'start' at
all. Certainly at the moment you're throeing away the first line.

Over all, how about the following skeleton program:

    use strict;
    use warnings;
    use Fcntl ':seek';

    my $filein = $ARGV[0];
    open FILEIN, $filein or die $!;

    sub start
    {
        for(;;) {
            build_db();
            $counter++;

            alert();
            $counter1++;

            print "$counter..$counter1\n";

            sleep 50;
        }
    }

    sub alert
    {
        seek FILEIN, 0, SEEK_SET;

        while( <filein> )
        {
            # does some thing on pattern matching
        }
    }

Note that the same 'seek' command needs to be before whatever code
you have in sub 'build_db'.

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to