in reply to Re: Missing the if statement and going directly to else
in thread Missing the if statement and going directly to else

The part number is declared earlier in the script. It is user input. The part number is in this format 68-2208-04. It worked before I put the else statement in there. I was trying to get the program to go to a different sub if the user enters a part number that is not in the text file.
  • Comment on Re: Re: Missing the if statement and going directly to else

Replies are listed 'Best First'.
Re: Missing the if statement and going directly to else
by tadman (Prior) on Jan 16, 2003 at 23:47 UTC
    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.