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

Hi monks

I'm a relative Perl novice and am trying to write a piece of code which will parse information from a file in this format:

Name(tab)Signal(tab)Present or Absent (P or A) call

Basically it does this by incremeting present or absent counts in anonymous arrays existing as the values in a DBM hash. It then prints the present/absent calls out to two files.

The problem is that the hash seems to basically disappear when I exit the <while> loop which parses the input file and my two output files remain empty.

I'm stumped - any help would be sincerely appreciated.

Example input (1 present and 1 absent call):

sequence1\t7\tP\n

sequence2\t1\tA\n

Code:

#!/usr/bin/perl use warnings; $input = "infile"; $background = "2"; open (INPUT, $input) or die "Cannot open infile!$!"; dbmopen (%PAHASH, "crDB", 0777) or die "Cannot open DBM file!$!"; while (<INPUT>) { next if (/^Gene/) || (/^AFFX/) || (/^2000/); chomp (@linearray = split "\t", $_); $name = shift @linearray; $signal = shift @linearray; $affycall = shift @linearray; #$name =~ s/_.{2,4}$//; if ($affycall eq "P" && $signal>$background) { $$PAHASH{$name}[0]++; } else { $$PAHASH{$name}[1]++; } } open (PRESENT, ">present"); open (ABSENT, ">absent"); foreach $key (keys %PAHASH) { if (defined $$PAHASH{$key}[0]) { print PRESENT "$key\t($$PAHASH{$key}[0] present calls)"; } else { print ABSENT "$key\t($$PAHASH{$key}[1] absent calls)\n"; } } dbmclose %PAHASH; exit;

Replies are listed 'Best First'.
Re: Problem with DBM hash of anonymous arrays
by Joost (Canon) on May 17, 2005 at 11:05 UTC
      The PHASH isn't anonymous though, is it? It's the named handle of the DBM hash. I think :-/
        There is no "PHASH". There is a $PAHASH, which is a scalar containing a reference to an anonymous hash and there is a %PAHASH, which is a hash (tied to a dbm file), but is unrelated to $PAHASH.

        If you do $PAHASH{$something} you're refering to %PAHASH. If you do $$PAHASH{$something}, you're referering to the hash referenced by $PAHASH. This can be confusing in the beginning, I know.

        Take a look at perlreftut. But most of all, get in the habit of using strict. It'll save you from a lot of bugs.