Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching a flat file

Status
Not open for further replies.

MAWarGod

Programmer
Feb 15, 2002
352
US
The file:

Invoice|Process Date|Tracking Number|Ship Method
00000000002|December 23, 2002|1z1234567890|UPS Ground
00000000003|December 23, 2002|1z1234567870|UPS Ground
10000000003|December 23, 2002|1z1234585870|UPS Ground
10008000003|December 25, 2002|1r1234585870|UPS Ground

I want to search the invoice # (first entery)
Then if a match have the following feilds print up

#!/usr/bin/perl

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer); foreach $pair (@pairs) {

($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",
hex($1))/eg;
$value =~ s/\:\://ig;

if ($INPUT{$name}) {
$INPUT{$name} = $INPUT{$name}.",".$value;
} else { $INPUT{$name} = ($value); }
for ($INPUT{'$name'}) {
s/^\s+//;
s/\s+$//;
}
}


#parse
print "Content-type:text/html\n\n";
open(DATA, &quot;data/dat.dat&quot;);$DATA = <DATA>;close DATA;
print &quot;$DATA&quot;;
($invoice,$processdate,$trackingnum,$shipmethod) = split(/\|/,$DATA);
if ($INPUT{'search'} eq $invoice) { print &quot;$invoice has been found.
<br>$invoice
<br>$processdate
<br>$trackingnum
<br>$shipmethod
<br>$ups
&quot;; } else { print &quot;No match found&quot;; }

print &quot;$INPUT{'search'} is the search - $invoice is not a correct invoice Please Hit back and retype the invoice Correctly&quot;;



What am I doing wrong?
 
Rather than dissect your code, I kind'a rewrote it a little.
Hopefully, with the code and it's comments, you can figure out what was wrong with yours.

Code:
#!/usr/bin/perl
use strict;
use CGI;
# create an object
my $cgi_object = new CGI;

# use the 'param' method to retrieve the value associated
# with the key 'search'
my $search = $cgi_object->param('search');

my $hit;

# start the html output.
print qq(Content-type: text/html\n\n),
      $cgi_object->start_html,
      qq(<p>Search for invoice: $search\n);


# read the data file into an array.
open(DATA, &quot;dat.dat&quot;) or die &quot;Failed to open DATA file, $!\n&quot;;

# while getting lines from the text file, 
# check each line for invoice num match.
# if match - print info and exit the while 
# with 'last'
# if no match, try the continue the 'while' to
# check the next line.
while (<DATA>) {
    if (/^$search\|/) {
        my @array = split /\|/, $_ ;
        print &quot;<br />INV : $array[0]\n
               <br />Date: $array[1]\n
               <br />Track:$array[2]\n
               <br />Ship Method: $array[3]\n
               </p>&quot;;
        $hit = 'yup';
        last;
    }
}
close DATA;

# unless we got a hit, gripe about it.
unless ($hit) { print &quot;No match found</p>&quot;; }

# close out the HTML 
print $cgi_object->end_html;
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top