in reply to processing key value pairs of a hash

You only show 2 columns of input data, but your code seems to expect 10 columns ($fields[9]). If I just assume 2 columns (what choice do I have?), you could stuff your input into an Hash-of-Hashes data structure as follows:
use strict; use warnings; use Data::Dumper; my %clone_hash; while (<DATA>) { my ($key, $value) = split; my $grade = ($value =~ /^pass[1-3]$/) ? 'good' : 'bad'; $clone_hash{$key}{$grade}++; } print Dumper(\%clone_hash); __DATA__ Eif2b2 fail Eif2b2 pass5 Eif2b2 fail Eif2b2 pass2 Eif2b2 fail Eif2b2 pass4 Eif2b2 fail 49334 fail 49334 fail 49334 pass1 49334 fail 49334 pass4 Oxct1 pass4 Oxct1 fail Oxct1 pass4 Oxct1 fail

which prints out (unsorted):

$VAR1 = { 'Eif2b2' => { 'good' => 1, 'bad' => 6 }, '49334' => { 'good' => 1, 'bad' => 4 }, 'Oxct1' => { 'bad' => 4 } };

Replies are listed 'Best First'.
Re^2: processing key value pairs of a hash
by lomSpace (Scribe) on Apr 15, 2009 at 15:34 UTC
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %clone_hash; while (<DATA>) { chomp; my @fields = split /\t/; my ($gene_symbol, $esc_qc) = ($fields[0], $fields[1]); $clone_hash{$gene_symbol} = $esc_qc; my $grade = ($esc_qc =~ /^pass[1-3]$/) ? 'good' : 'bad'; $clone_hash{$key}{$grade}++; } print Dumper(\%clone_hash); _DATA_ 4933402D24Rik fail 4933402D24Rik fail 4933402D24Rik fail 4933402D24Rik fail 4933402D24Rik fail 4933402D24Rik fail 4933402D24Rik fail 4933402D24Rik pass1
    When I run this I get an error message stating that a
    bareword is found where an operator is expected near
    "4933402D24Rik"(missing operator before "D24Rik"),
    what does this mean?
      I realized that I did not use two underscores before and after DATA.
Re^2: processing key value pairs of a hash
by lomSpace (Scribe) on Apr 15, 2009 at 14:16 UTC
    Toolic
    I created an empty hash, then read in a file, split it and used
    fields1 and fields4 to create key value pairs. I then sorted the list by keys.
    I want the following type of output:
    Clone Good Single Clone
    Eif2b2 1 1

    Thanks the hash of hashes answers the first part, but how do
    I print the value from the 'good' key in the hash of hashes?

    Thanks
    LomSpace
      but how do I print the value from the 'good' key in the hash of hashes?
      Loop through the keys of the primary hash, and print out the 'good' values if 'good' keys exist:
      use strict; use warnings; use Data::Dumper; my %clone_hash; while (<DATA>) { my ($key, $value) = split; my $grade = ($value =~ /^pass[1-3]$/) ? 'good' : 'bad'; $clone_hash{$key}{$grade}++; } #print Dumper(\%clone_hash); # Print out the 'good' keys for (keys %clone_hash) { print "$_ => $clone_hash{$_}{good}\n" if exists $clone_hash{$_}{go +od}; } __DATA__ Eif2b2 fail Eif2b2 pass5 Eif2b2 fail Eif2b2 pass2 Eif2b2 fail Eif2b2 pass4 Eif2b2 fail 49334 fail 49334 fail 49334 pass1 49334 fail 49334 pass4 Oxct1 pass4 Oxct1 fail Oxct1 pass4 Oxct1 fail

      which prints:

      Eif2b2 => 1 49334 => 1
        toolic
        Much thanks!
        LomSpace
        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