[thelist] Perl file reading problem.....
Keith
cache at dowebscentral.com
Sat Jun 15 11:09:01 CDT 2002
At 06:22 PM Friday 6/14/2002, CDitty wrote:
>Hello all. I am trying to read data in from a text file and if one 2
>conditions are met, increment a counter. But I am having problems. Half
>of it seems to work, but the rest doesn't. All the needed variables are
>set by the time it reaches this section.
>
>Here is the file reading code.
># bob at bob.com|165|0|000| This line is how the text file looks.
>
><code>
>open (LOGCOUNT, "crack_log.txt") || die "Can't open crack_log.txt file for
>reading.";
>flock (LOGCOUNT, 2); # Locks the file
>$logcountfile = <LOGCOUNT>; # Assigns the data from the file into an array
> # bob at bob.com|165|0|000|
> # email address, Play day, win/loss, guess
> ($emailid,$play) = split(/\|/, $logcountfile);
> if(($emailid == $emailaddress) && ($play == $play_day)){
> $total_plays++;
> }
>flock (LOGCOUNT, 8); # Unlocks the file
>close(LOGCOUNT);
></code>
3 problems
1) need to loop through the file
2) using numeric (==) comparison instead of string comparison (eq)
3) end loop (assuming large file)
<correct code>
open (LOGCOUNT, "crack_log.txt") || die "Can't open crack_log.txt file for
reading.";
lock (LOGCOUNT, 2); # Locks the file
while(<LOGCOUNT>){
chomp();
($emailid,$play) = split(/\|/);
if(emailid eq $emailaddress){
if($play eq $play_day){
$total_plays++;
}
last;
}
}
flock (LOGCOUNT, 8); # Unlocks the file
close(LOGCOUNT);
</correct code>
Keith
====================
cache at dowebscentral.com
More information about the thelist
mailing list