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: Intermediate:
combining 2 scripts

 



yim11
Novice

Dec 12, 2000, 7:31 AM

Post #1 of 3 (455 views)
combining 2 scripts Can't Post

Hello,
I have 2 scripts - catscan.pl (posted below) and receiving.pl (not posted). Catscan.pl when run waits for a scan from a cuecat barcode scanner and converts the scanned barcode to a id number. Receiving.pl takes users input (id number) and inserts that number into a database.

What I would like to have is one script that would allow a user to scan a barcode and the script would convert that scan into a id and insert it into the database.

I'm not even sure if this can be done, but if so, I would be grateful to any pointers to resources to help!

TIA!!
Jim

catscan.pl
----------Begin Code----------------
#!/usr/bin/perl5
# divide the UPC into 4 groups of three digits
# use the scheme below to translate each digit into its output
#
# 1 2 3
#
# 0 C3 n Z
# 1 CN j Y
# 2 Cx f X
# 3 Ch b W
# 4 D3 D 3
# 5 DN z 2
# 6 Dx v 1
# 7 Dh r 0
# 8 E3 T 7
# 9 EN P 6
%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);

%types = ('cGf2', 'ISBN', 'cGen', 'ISBN', 'fHmg', 'UPC', 'fGzX', 'UPC-E1');

while(<>) {

exit if $_ eq "\n";

if (!/^\.(.*)\.(....)\.(.*)\.$/) {
print "invalid code\n";
exit;
}


$id = $1;
$type = $2;
$code = $3;

$output = "";
while ($code =~ /^(..)(.?)(.?)(.*)/) {
$output = $output . $translation{$1};
$output = $output . $translation{$2};
$output = $output . $translation{$3};
$code = $4;
}


if ($types{$type} ne "") {
print "$types{$type} ";
}

print "$output\n";

}
----------End Code------------




rGeoffrey
User / Moderator

Dec 12, 2000, 12:51 PM

Post #2 of 3 (447 views)
Re: combining 2 scripts [In reply to] Can't Post

Combining scripts depends on how the two originals interact. But making a few guesses from what I see here I have a function that you should be able to drop into the second script and call from there at the place where you are probably now reading from STDIN


Code
sub Get_CueCat_Input 
{
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);
}

Calling &Get_CueCat_Input () will get you an array of the values that you were probably expecting from the first script.

I have made some changes inside the code that are worth mentioning.

The two hash declarations now make use of the '=>' notation so we don't have to put the keys in quotes.

The "if (!something)" has been changed to "unless (something)" because I like unless and don't like '!'.

The first 'exit' is now a 'next' and the second 'exit' is gone. These are done because we are now in a subroutine of a much larger program and not in a stand alone script. Thus the stuff that was after the second exit is now in the else. This should allow us to jump over bad input without killing the program.

$id is gone. It was never used.

"$output = $output . something" has been simplified.

'if ($something ne "")' is the same thing as saying 'if ($something)' so I simplified it also.

And rather than print the answers to STDOUT, we now push them onto the array that will be returned to the code currently known as Receiving.pl (the second script). I have left the "\n" at the end of each element to mimic your code, but it is probably better in the long run to drop the "\n" here. You can always add it back in when printing.



yim11
Novice

Dec 14, 2000, 10:42 AM

Post #3 of 3 (428 views)
Re: combining 2 scripts [In reply to] Can't Post

Thank You very much for your help!

I have another question if you don't mind...

I kind of see what your doing here, but what I cant figure out is exactly how to call that subroutine - well more precisly - WHEN to call it...

receiving.pl creates a webpage with 2 user input fields - filenumber and quantity.
If i put the cursor in the filenumber field and scan a barcode w/the cuecat it inserts its cryptic code into the filenumber field and the cuecat seems to have a embedded carrige return as a submit is always entered after scanning a barcode (which is fine for me) - where I'm confused is how to call the subroutine after the cuecat has scanned the barcode so that receiving.pl sees the decoded filenumber and not the cryptic cuecat code.

I know I should post receiving.pl - but its kind of long - as it deals with a lot of DB insertions... will be happy to post if needed though!

TIA again for all your help!
Jim


 
 


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

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