g man has asked for the wisdom of the Perl Monks concerning the following question:

My apologies, I am a beginner. Below is my mangled code. I am having trouble finding out how to read from the infile in the subroutine, so that i can find the elements from each line contained in the hash table to combine them into one unit, and create a code. Again...you will have to forgive me for my rather basic skills. Code starts here:
require hash.pl $infile="specimen100.txt" open(IN,"<$infile"); while ($line=<IN> { tr/[A-Z]/[a-z]/; #lowercase all characters s/\s+/ /; #remove extra spaces @sample=split(/[0-9][.], $line); for each (@sample) { $code=&encode($_); last if ($code); } print "code \n"; } sub encode { my(@sample=@_); $site=&findsite($sample); $specimen=&findtissue($sample); $procedure=&findproc($sample); my($code)=&assigncode($site,$specimen,$procedure); $code; } #in the subroutine below, i am trying to match the input with a hash t +able to find longest #common procedure, tissue, or site word #combine all three together to form a new code sub findproc { foreach $key (sort keys %procedure){ if ($_ =~ /$key/){ print "$procedure{$key}\n"; } } sub findtissue { foreach $key (sort keys %specimen){ if ($_ =~ /$key/){ print "$specimen{$key}\n"; } } sub findsite { # ditto for site foreach $key (sort keys %site){ if ($_ =~ /$key/){ print "$site{$key}\n"; } } if ($code) { print STDERR "$code $line"; } else { print $line; } close (IN);

Replies are listed 'Best First'.
Re: problems with filehandle and more
by chromatic (Archbishop) on Apr 19, 2000 at 19:21 UTC
    I managed to clean up half of it, running with -w and use strict. I advise everyone to use those for any script longer than ten lines or any program you're going to use more than twice. It's also a good way to learn better technique.

    Here's what I came up with:

    #!/usr/bin/perl -w use strict; my $infile="specimen100.txt"; open (IN, "$infile") || die "Can't open $infile: $!"; while (<IN>) { my $code = ""; tr/[A-Z]/[a-z]/; #lowercase all characters s/\s+/ /; #remove extra spaces my @sample=split(/[0-9][.]/, $_); foreach my $sample (@sample) { $code=&encode($sample); last if ($code); } print "$code \n"; } sub encode { my $sample = shift; my $site=findsite($sample); my $specimen=findtissue($sample); my $procedure=findproc($sample); return assigncode($site,$specimen,$procedure); }
    There were a lot of typos -- did you cut and paste your code, or did you retype it? Doing the former is the best way to get help.

    As to your question, the biggest problem I see is that you're not passing a hash into your subroutines. Unless you have global hashes %procedure, %site, and %specimen defined in hash.pl, they're not defined anywhere.

    Your process might work (depending on the contents of hash.pl) if you were to rewrite each subroutine as follows:

    sub findproc { my $possible_key = shift; # get the argument passed to this sub if (defined $procedure{$possible_key}) { print $procedure{$possible_key}, "\n"; } }
    This does two things. First, it grabs the argument passed to each subroutine. (While @_ is the list of arguments, $_ is not the first argument. Get at it with shift, which operates by default on @_ in a subroutine.) Second, it takes advantage of a hashtable. If the information from your inputfile specifies the keys of the hash exactly, you don't have to iterate through the list of hash keys. (If they're not exact, well, the procedure you're using will work, though there are better ways to do it.)

    Make sense?