CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
input help needed

 



yim11
Novice

Dec 19, 2000, 7:44 AM

Post #1 of 3 (283 views)
input help needed Can't Post

Hello!
I have the following script, that almost works as it should, but I have to change the input for a section of the script, and don't know how. I have posted the code below and commented the section that I am having troubles with.

Thanks in advance for any and all help!
Jim

---Begin Code---
#!/usr/bin/perl


# CGI routines
use CGI_Lite;

my $cgi = new CGI_Lite;
my %in = $cgi->parse_form_data;

# get barcode scan from input
my $scan = $in{"SCAN"};

if ($scan)
{
my $translation = translate($scan);
print "Content-type: text/html\n\n";
print "Product ID is: $translation";
}
else {prompt();}

sub translate
{
my ($scan) = @_;

# do your translation code here with $scan as the input
#this is the section I need help with

my @outputs;
my %translation = (C3 => 0, n => 0, Z => 0,
CN => 1, j => 1, Y => 1,
Cx => 2, f => 2, X => 2,
Ch => 3, b => 3, W => 3,
D3 => 4, D => 4, 3 => 4,
DN => 5, z => 5, 2 => 5,
Dx => 6, v => 6, 1 => 6,
Dh => 7, r => 7, 0 => 7,
E3 => 8, T => 8, 7 => 8,
EN => 9, P => 9, 6 => 9);
my %types = (cGf2 => 'ISBN', cGen => 'ISBN', fHmg => 'UPC', fGzX => 'UPC-E1');
while(<>) {

next if ($_ eq "\n");
unless (/^\.(.*)\.(....)\.(.*)\.$/) {
print "invalid code\n";
} else {
my $type = $2;
my $code = $3;
my $output = "";
while ($code =~ /^(..)(.?)(.?)(.*)/) {
$output .= $translation{$1} . $translation{$2} . $translation{$3};
$code = $4;
}
push (@outputs, (($types{$type}) ? "$types{$type} " : '') . "$output\n");
}
}
return (@outputs);
}

sub prompt
{
print "Content-type: text/html\n\n";
print qq~
<html><body>
<form method="post" action="cat2.pl">
<input type="text" name="SCAN">

<input type="submit">
</form>
</body></html>
~;
}
---End Code---




rGeoffrey
User / Moderator

Dec 19, 2000, 9:51 PM

Post #2 of 3 (271 views)
Re: input help needed [In reply to] Can't Post

Rather than send $scan to &translate and then expect &translate to read more data, you could do the reading outside and send &translate(@input) a whole array of the data you got from the scanner.

Then inside &translate replace...

while (<>)

with

foreach (@input)

I will leave filling @input to you as you know how the scanner works.



zanardi
journeyman

Dec 20, 2000, 9:58 PM

Post #3 of 3 (266 views)
Re: input help needed [In reply to] Can't Post

Since you've intialized CGI.pm you wont be needing these:

print "Content-type: text/html\n\n";

some where after:

my $scan = $in{"SCAN"};

I'd put:

print $cgi->header;



My BBS

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives