in reply to Re^3: processing key value pairs of a hash
in thread processing key value pairs of a hash

Hi toolic!
I have tried several different ways to trying to get the 'bad' values from
the hash of hashes. Some compile and others don't. The lit online is not
clear to me. In my code you will see where I commented out my most
recent attempt at printing the 'bad'.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; =cut open( my $in, "c:/Documents and Settings/mydir/Desktop/current/mart_ex +port.txt" ); open( my $out, ">c:/Documents and Settings/mydir/Desktop/current/clone +_qual_counts.txt" ); my $first_line = <$in>; chomp $first_line; my %clone_hash; print $out "clone\tgood\tbad\n"; while (<$in>) { chomp; my @fields = split /\t/; my ($gene_symbol, $esc_qc) = ($fields[1], $fields[8]); #use regex to extend to a hash of hashes to separate good and bad +clone counts my $grade = ($esc_qc =~ /^pass[1-3]$/) ? 'good' : 'bad'; $clone_hash{$gene_symbol}{$grade}++; } for (keys %clone_hash) { #print $out "$_ \t $clone_hash{$_}{good}\n" if exists $clone_hash{ +$_}{good}; print $out "$_ \t$clone_hash{$_}{good}\n" if exists $clone_hash{$_ +}{good}; #print $out "\t$clone_hash{$_}->{'bad'}\n"; } #print Dumper(\%clone_hash); close ($in); close ($out);
It is apparent that I am new to hash of hashes. I humbly appreciate you help.
LomSpace

Replies are listed 'Best First'.
Re^5: processing key value pairs of a hash
by toolic (Bishop) on Apr 15, 2009 at 19:27 UTC
    untested...
    for (keys %clone_hash) { my $gcnt = (exists $clone_hash{$_}{good}) ? $clone_hash{$_}{good} +: 0; my $bcnt = (exists $clone_hash{$_}{bad} ) ? $clone_hash{$_}{bad} +: 0; print $out "$_\t$gcnt\t$bcnt"; }

    Update: removed erroneous "exists"

      Hi toolic!

      That was simple enough! Thanks for the introduction to hash of hashes.

      Thanks!

      LomSpace