Greetings,
Once again I must turn to the Monks for some help. The code below is meant to search a text file and it does not do it, but I feel that I am close.
I have a text file named fai.txt The information in it is laid out as follows;
blah|blah|blah|blah
this|this|this|this
the fields are all seperated by |
when a part number is entered I want to search the text file and if the first field matches the part number entered then I want that entire string to be printed to the screen.
One of the error messages I get is that "Global symbol @fields requires explicit package name"
I though I declared it in the subroutine. I would appreciate any poke in the right direction on this.
Thanks,
Kerry
print "What is the Part Number that you would like to look up?\n";
my $PartNumber = <STDIN>;
chomp($PartNumber);
while (!$PartNumber){ ## while $PartNumber is empty keep asking
print "PartNumber? ";
$PartNumber = <STDIN>;
chomp($PartNumber);
}
&search;
#this is the subroutine to search the file
sub search{
open(FILE, '>>fai.txt') || die $!;
while (<FILE>) {
chomp; # remove newline
my @fields = split(/\|/, $_);
# test whether the search string matches part number
if ($fields[0] =~ /$PartNumber/ ) {
print "$PartNumber Rev. $Revision has a First Article
+Report\nWould you like to view it?";
}
}}
close(FILE);
my $answer = <STDIN>;
chomp($answer);
if ($answer eq "yes") {
print "\n\n\none second please......\n";
print "retrieving file........\n";
<b>error caused by this--></b> print "$fields[0]: $fields[1]: $fields[
+2]: $fields[3]\n";
}
# If the answer is no then die
else {
print "ok, well have a nice day.\n";
}
credo quia absurdum
update (broquaint): s|<(/)?pre>|<${1}code>|g
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.