Given that you have two sets of non-numeric keys, I would think a hash of hashes is a more logical structure than a hash of arrays. I would also, as Athanasius suggests, separate your data structure construction from your output process. Essentially, build a hash of hashes, create sorted lists of your genes and transcription factors, and then print the table:
#!/usr/bin/perl use strict; use warnings; use List::MoreUtils qw(uniq); use 5.10.0; my %gene2TF2val; while (<DATA>) { my ($gene, $tf) = split; $gene2TF2val{$gene}{$tf}++; } # ID distinct transcription factors, sorted by value my @tf = sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0]} uniq map keys %$_, values %gene2TF2val; # Print table header say join "\t", "NAMES", @tf; # Print table contents for my $gene (sort keys %gene2TF2val) { say join "\t", $gene, map $_ ? '+' : '-', @{$gene2TF2val{$gene}}{@tf}; } __DATA__ geneA T1 geneA T1 geneA T2 geneB T8 geneC T10 geneC T1
Note that the command line switch -w is (mostly) synonymous with use warnings. If you are not yet comfortable with map and Slices, you can store intermediate results in arrays:
#!/usr/bin/perl use strict; use warnings; use List::MoreUtils qw(uniq); use 5.10.0; my %gene2TF2val; while (<DATA>) { my ($gene, $tf) = split; $gene2TF2val{$gene}{$tf}++; } # ID distinct transcription factors, sorted by value my @tf; for my $tf (values %gene2TF2val) { push @tf, keys %$tf; } @tf = sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0]} uniq @tf; # Print table header say join "\t", "NAMES", @tf; # Print table contents for my $gene (sort keys %gene2TF2val) { print $gene; for my $tf (@tf) { my $has = $gene2TF2val{$gene}{$tf} ? '+' : '-'; print "\t$has"; } print "\n"; } __DATA__ geneA T1 geneA T1 geneA T2 geneB T8 geneC T10 geneC T1

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: transforming a table by kennethk
in thread transforming a table by v15

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.