×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help needed with a subroutine please

Help needed with a subroutine please

Help needed with a subroutine please

(OP)
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---

RE: Help needed with a subroutine please

to get $scan in to the sub...


sub convert
{
   my $scan = shift;

   return $output;
}

adam@aauser.com

RE: Help needed with a subroutine please

(OP)
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

RE: Help needed with a subroutine please

What you need is to "receive" the $output variable back from the call to "convert", like this:

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

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

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


There are really 2 ways to handle this - the simplest way is as I've described above, whereby the subroutine "return"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 "receive" 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 "perldoc perlref".

HTH.


Hardy Merrill
Mission Critical Linux, Inc.
http://www.missioncriticallinux.com

RE: Help needed with a subroutine please

(OP)
Thanks very much! Everything is good to go!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close