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?


In reply to Re: problems with filehandle and more by chromatic
in thread problems with filehandle and more by g man

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.