in reply to Missing the if statement and going directly to else

There might be some regexp-type characters in your $PartNumber variable. To make sure they don't screw anything up, you might want to do this:
if ($main::fields[0] =~ /\Q$PartNumber\E/) { # ... }
The case might be that $PartNumber contains, for example, brackets.

Also, where does $PartNumber originate? It looks like a global, though if you're not using strict you never really know.

Although it's not strictly important to the way your program operates, I'd suggest that using brackets when calling functions is preferable to ampersands. For example:
continue();
Perl 4 required ampersands, so they're left in there for compatibility reasons, mostly, but are otherwise deprecated.

Update:
Based on your earlier post I think that you might be able to do a straight eq comparison instead of a regular expression match, provided you're typing in the literal part number, character for character.
if ($main::fields[0] eq $PartNumber) { # ... }

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