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:
Help needed with a subroutine please

 



yim11
Novice

Feb 6, 2001, 7:51 AM

Post #1 of 1 (309 views)
Help needed with a subroutine please Can't Post

Hello,
I have the following script shown below that ~should~ convert a string of text. The conversion subroutine works fine, I just cannot figure out how to pass the $scan variable into the subroutine and get the sub to return the converted string ($output).
The $scan variable comes from a html form, it is a textfield that contains a string of characters. I know the form is passing the string to the script as I have added a print $scan line that shows that it does get passed.

Any and all help on this is greatly appreciated. I have been working on this for a while and cannot get it working to save my life.

TIA!!!
-jim

---begin code---
#!/usr/bin/perl
#
# 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
#
#
use CGI;
$co = new CGI;
$scan=$co->param('scan');

print $co->header;
$co->start_html();
print "input is: $scan<P>";

convert($scan);
print "output is now: $output";

sub convert {

%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} ";
}

return ($output);


}
} #end sub

print "<P>output is: $output";
$co->end_html;
---end code---



 
 


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

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