Nabeel wrote:
Greetings, I need to be able to re-read a file every 60 seconds until
the two changing values are the same in my data file. Once the values
become the same the script simply exits. Not exactly sure if my
filehandle will reread itself every 60 seconds either but I keep getting
compilation errors, sigh.
The data file looks like this using the | as a delimiter:
720|800
My Code:
use warnings;
use strict;
open (FH, "<datafile.txt");
You should *always* verify that the file opened correctly.
open FH, '<', 'datafile.txt' or die "Cannot open 'datafile.txt' $!";
@values = split(/\|/, <FH>);
With strict in force you have to use lexical variables.
my @values = split /\|/, <FH>;
while ($values[0] != $values[1]) {
print "Not in sync...\n";
system("sleep 60");
Perl has a built-in sleep function:
perldoc -f sleep
}
exit 0;
Your problem is that the values do not change inside the loop. This
should work:
use warnings;
use strict;
use Fcntl ':seek';
open FH, '<', 'datafile.txt' or die "Cannot open 'datafile.txt' $!";
{ my @values = <FH> =~ /\d+/g;
last if $values[ 0 ] == $values[ 1 ];
print "Not in sync...\n";
sleep 60;
seek FH, 0, SEEK_SET or die "Cannot seek on 'datafile.txt' $!";
redo;
}
exit 0;
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/