in reply to problems with filehandle and more

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?