I took a somewhat similar approach to the other reponders and used a Hash of Hashes (HoH) and combined the two data values for each entry into a string which could latter be split() so as to avoid the third level of deep data structure.

My approach (with just the principle code snippet) is shown below:

my %myHash = (); my %tempHash = (); foreach (@lines){ my($key1,$key2,$val1,$val2,$rest) = split(/\s+/,$_,5); my $combinedValue = sprintf("%2s,%2s",$val1,$val2); $key1 =~ /SNP(\d+)/; my $indx = $1; if(exists $myHash{$key2}){ %tempHash = %{$myHash{$key2}}; $tempHash{$indx} = $combinedValue; $myHash{$key2} = {%tempHash}; } else { $tempHash{$indx} = $combinedValue; $myHash{$key2} = {%tempHash}; } } foreach my $key (sort keys %myHash){ my %tempHash2 = %{$myHash{$key}}; my $line2output = "$key "; foreach my $sortedKey (sort keys %tempHash2){ $line2output .= sprintf(" %2s %2s",split(',',$tempHash2{$sortedKey})); } print "$line2output\n"; }

I have also put the OP's example data input into an array, @lines, to simplify my testing. Assuming that the lines are being read in from a file, one would do a foreach (<INPUT>){} rather than my foreach (@lines){} structure.

I hope this helps and shows yet another approach that works. I didn't spend a lot of time optimizing or simplifying. I figure that is a worthwhile exercise for the reader and the OP.

ack Albuquerque, NM

In reply to Re: Table manipulation, array or hash? by ack
in thread Table manipulation, array or hash? by robertkraus

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.