Hi Monks,

How can I fix the following code (or may be rewrite) to print the keys and values of two hashes in a tabulated format. A simple operation also needed to be performed on values. The keys of two hashed are identical.

#! /usr/bin/perl -w use strict; my %hash_1 = ( 0.2 => '12', 0.4 => '13', 0.6 => '14', ); my %hash_2 = ( 0.2 => '19', 0.4 => '18', 0.6 => '20', ); foreach my $ObsKey ( sort { $a <=> $b } keys %hash_1 ) { foreach my $PermKey ( sort { $a <=> $b } keys %hash_2 ) { my $diff_obs_perm = $hash_1{$ObsKey}-$hash_2{$PermKey}; my $FDR = $hash_2{$PermKey} / $hash_1{$ObsKey}; my $currentdeltaprint = join ("\t", $ObsKey, $hash_1{$ObsKey}, $hash_2{$PermKey}, $diff_obs_perm, $FDR ); print "$currentdeltaprint\n"; } }

It seems I need to exit the inner loop after each printing. But it keeps printing until all the keys of the inner loop get exhausted and goes to the outer loop.

Thanks Monks.

In reply to Print Keys - values of two hashes in a tabulated fromat by sesemin

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.