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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.