If you're trying to print an error if the user enters a part number that is not in the file, and the file contains more than one line, then you are going to have to wait until you've finished reading the file before putting up the error.
I think your if is working correctly, the problem is simply that if you type in a part number that appears on line 2 of the file, you test line 1 first and fail, restarting.
Maybe what you mean is something like this:
sub search
{
my ($PartNumber) = @_;
while (<FILE>)
{
if ($main::fields[0] eq $PartNumber)
{
# ...
return 1; # Ends search() function
}
}
close(FILE);
return; # Returns empty handed
}
This search routine returns 1 if it found a match, undef otherwise. You can put this in a routine which asks for input, like this:
$|++;
my $what;
do
{
print "PartNumber? ";
chomp($what = <STDIN>);
} while (!search($what));
This loop will continue to run until the search() function returns a true value. That will only happen if a match is found. | [reply] [d/l] [select] |