Here's what I came up with:
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.#!/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); }
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:
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.)sub findproc { my $possible_key = shift; # get the argument passed to this sub if (defined $procedure{$possible_key}) { print $procedure{$possible_key}, "\n"; } }
Make sense?
In reply to Re: problems with filehandle and more
by chromatic
in thread problems with filehandle and more
by g man
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |