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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with a subroutine please

Status
Not open for further replies.

yim11

MIS
Jun 26, 2000
35
0
0
US
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 &quot;input is: $scan<P>&quot;;

convert($scan);
print &quot;output is now: $output&quot;;

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 &quot;\n&quot;;

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


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

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


if ($types{$type} ne &quot;&quot;) {
print &quot;$types{$type} &quot;;
}

return ($output);


}
} #end sub

print &quot;<P>output is: $output&quot;;
$co->end_html;
---end code---
 
to get $scan in to the sub...


sub convert
{
my $scan = shift;

return $output;
} adam@aauser.com
 
Thanks for the reply,
I added the my $scan = shift; at the beginning of the subroutine, and took the parens off the return $output, but still no luck getting it running. : (


Thanks for responding so quickly!
-jim
 
What you need is to &quot;receive&quot; the $output variable back from the call to &quot;convert&quot;, like this:

From:
-----
convert($scan);
print &quot;output is now: $output&quot;;

To:
---
my $converted_output = convert($scan);
print &quot;output is now: $converted_output&quot;;

Or, if you want to keep using $output:
----------------------------
$output = convert($scan);
print &quot;output is now: $output&quot;;


There are really 2 ways to handle this - the simplest way is as I've described above, whereby the subroutine &quot;return&quot;s the value of something, and the calling routine receives it by assigning it to a variable, like

$output = convert($scan);

The 2nd way is for the calling routine to pass the variable by reference - the subroutine receives the reference, and modifies the value that the reference points to. If done this way, the calling routine does NOT need to &quot;receive&quot; the value back, since the subroutine was changing the same $output variable that the caller is using. This might seem confusing, but it's actually pretty straight forward - there's a whole perldoc devoted to references - I think you can see it by doing &quot;perldoc perlref&quot;.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thanks very much! Everything is good to go!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top