Gossamer Forum
Home : General : Perl Programming :

How to check a file for a variable?

Quote Reply
How to check a file for a variable?
If I don't make any sense I've been trying everything I can find and Starting to get a little cross-eyed.

I'm working on this script and everything works fine except I cannot get a file check to return true for $input{'count'}.

Its a simple file with 1 word/number (no spaces, odd, charachters, etc.) per line. I can write $input{'count'} to the file but I want a different action to happen if its already in the file.

I'm a newbee looking at scripts and figuring out some of whats going on and how, this has got to be simple but so far no luck.
Quote Reply
Re: How to check a file for a variable? In reply to
 
Quote:
I cannot get a file check to return true for $input{'count'}.

What do you mean here? Some sample code would be great!

Cheers,

Alex
Quote Reply
Re: How to check a file for a variable? In reply to
OK I've gotten somewhere. I had been trying to use else inside of foreach (tried while also) which is why it would only find a match if $input{'count'} was on the first line of count.txt.

Code:
sub postnow {
open (count2, "<count.txt") or &error("Unable to open the counting file");
if ($flock eq "y") {
flock count, 2;
}
@count2=<count2>;
close (count2);
$line=0;
foreach $count2 (@count2) {$line++;
if ($count2=~ /$input{'count'}/i) {$line--;
print "TRUE\n";
exit;
}
}
print "FALSE\n";
exit;
}

The print statements will be replaced with aprox. 60 lines of code each. It's cobled together with only minimal understanding. I'm sure there's either a better way or something wrong with it.

...Got rid of the $line references. Not sure exactly how it works but in the code I stole it from it looks like the line matching $input{'count'} was being removed/replaced?? with the contents of "foo", rewriting the file to do this??

@reply="foo\n";
splice (@count2, $line, 1, @reply)\n;

open (count3,"count.txt")\n;
print count3 @count2\n;
close (count3)\n;

Think I got that right (condensed version).
Interesting stuff.

[This message has been edited by Dave (edited May 04, 1999).]