Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Printing out multiple hashes at one time

by cvillain (Initiate)
on Dec 19, 2012 at 19:44 UTC ( #1009615=perlquestion: print w/replies, xml ) Need Help??
cvillain has asked for the wisdom of the Perl Monks concerning the following question:

Before getting to my question, I feel compelled to make the following statements:

- I'm new to Perl.

- There is probably a better/easier way to do this.

- Thanks in advance for your help!

I have long text file with DNA pentamers (i.e. ATGCA) and associated data (number observed, expected probability, total number of pentamers in the sequence) for each pentamer. The pentamers are listed multiple times and I would like to create a way to combine all repeated entries.

I have written a script that creates new hashes for each value and successfully combines repeated entries.

Now what I would like to do is print out on one line the following:

Pentamer \t Probability \t totalCounts \t totalPentCount \n

(spaces added for readability)

I am at a total loss for what to do next and I would appreciate advice if I should give up and try another way or keep going with what I have...

#!/usr/bin/perl use strict; use warnings; #given a completed pentamer count file, sort pentamers, and consolidat +e duplicates my @data; my @currentPentLine; my $counter; my $pent; my $pentExpected; my $pentObserved; my $pentTotal; #read in input file, capturing pentamers and related info for each open(FREQ,"<testPentamerCounts.txt") or die "Cannot open FREQ output f +ile\n"; while (<FREQ>) { chomp; @currentPentLine = split("\t", $_); $pent = $currentPentLine[0]; $pentExpected = $currentPentLine[1]; $pentObserved = $currentPentLine[2]; $pentTotal = $currentPentLine[3]; #create array of hashes with data from input file $data[$counter]{'pent'} = $pent; $data[$counter]{'count'} = $pentObserved; $data[$counter]{'prob'} = $pentExpected; $data[$counter]{'pentTotal'} = $pentTotal; $counter++; } close FREQ; my $criteria = "pent"; my @sorted_pentamers = sort { $a->{$criteria} cmp $b->{$criteria} } @d +ata; my %totalProbability; my $probTotal; for $probTotal (@sorted_pentamers) { if (not exists $totalProbability{$probTotal->{pent}}) { $totalProbability{$probTotal->{pent}} = 0; } $totalProbability{$probTotal->{pent}} += $probTotal->{prob}; } my %totalCounts; my $countTotal; for $countTotal (@sorted_pentamers) { if (not exists $totalCounts{$countTotal->{pent}}) { $totalCounts{$countTotal->{pent}} = 0; } $totalCounts{$countTotal->{pent}} += $countTotal->{count}; } my %totalPentCounts; my $pentTotal; for $pentTotal (@sorted_pentamers) { if (not exists $totalPentCounts{$pentTotal->{pent}}) { $totalPentCounts{$pentTotal->{pent}} = 0; } $totalPentCounts{$pentTotal->{pent}} += $pentTotal->{pentTotal +}; } my @keysProb = keys %totalProbability; my @keysTotal = keys %totalCounts; print @keysProb, "\n"; print @keysTotal, "\n"; #print unique sequences to output file Is there some way I could access iterate through each pentamer as a ke +y in each of the hashes and print out each value? Thanks again!

Replies are listed 'Best First'.
Re: Printing out multiple hashes at one time
by roboticus (Chancellor) on Dec 19, 2012 at 20:14 UTC


    If you'd fix some of the messages that strict and warnings give you, it might simplify things. But as far as your question: Yes you can scan through the keys and print them like this:

    for my $penta (keys %totalProbability) { print "$totalProbability{$penta}, $totalCounts{$penta}, ...\n"; }

    However, the reason I replied is to mention this: You'd do yourself a favor if you build the statistics data you need as you read it. Something like:

    my %stats; while (<FREQ>) { chomp; my ($pent, $pentExpected, $pentObserved, $pentTotal) = split("\t", +$_); next if !defined $pentTotal; # skip malformed lines and end line my $h = { pent=>$pent, count=>$pentObserved, prob=>$pentExpected, pentTotal=>$pentTotal }; push @data,$h; if (exists $stats{$pent}) { $stats{$pent} = { probTotal => $$h{prob}, countTotal => $$h{count}, pentTotal => $$h{pentTotal}, }; } else { $stats{$pent}{probTotal} += $$h{prob}; $stats{$pent}{countTotal} += $$h{count}; $stats{$pent}{pentTotal} += $$h{pentTotal}; } }


    When your only tool is a hammer, all problems look like your thumb.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009615]
Approved by davido
NodeReaper serves mincemeat tarts with the cider

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-06-23 03:29 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.