Hi homeveg,

I think you will want to do this with a couple of passes.

In the first pass, read each ID, and keep track of how many columns each table has, as well as saving individual values per table.

In the second pass, for each ID, collect the corresponding values from each table, or assign the appropriate number of "n.a." (if the ID isn't defined for the table).

For example:

# Strict use strict; use warnings; # Libraries use Data::Dumper; # User-defined + # Define tables, each one a separate value in the hash "$ptables" my $ptables = { 'tab1' => [ "ID column | column 1", "gene 1 | value 1.1", "gene 2 | value 2.1", "gene 4 | value 4.1", "gene 8 | value 8.1", ], 'tab2' => [ "ID column | column 1 | column2", "gene 1 | value 1.1 | value 1.2", "gene 3 | value 3.1 | value 3.2", "gene 4 | value 4.1 | value 4.2", ] }; # Globals my %output; my %ncolumns; my %values; my @tables = (sort keys %$ptables); # Get all table na +mes # Main program # First pass -- parse each table to fetch all the IDs print "=== Pass 1 ===\n"; foreach my $table (@tables) { my $ptab = $ptables->{$table}; # Assign to table my @rows = split(/\s*\|\s*/, shift @$ptab); # Get column headi +ngs shift @rows; # Discard "ID colu +mn" my $ncols = @rows; # Find number of c +olumns $ncolumns{$table} = $ncols; # Save # of column +s print "Reading $table; $ncols col(s)...\n"; # Announce table n +ame foreach my $line (@$ptab) { my ($id,@vals) = split(/\s*\|\s*/, $line); # Get ID and value +s $output{$id} ||= [ ]; # Placeholder for +ID $values{$table}{$id} = [ @vals ]; # Save values for +table/ID } } # Second pass -- process each ID, adding values from each table my @ids = (sort keys %output); print "=== Pass 2 ===\n"; foreach my $id (@ids) { print "Processing ID $id\n"; my $pout = $output{$id}; # Get current ID l +ist foreach my $table (@tables) { my $ncols = $ncolumns{$table}; # Get number of co +lumns my $pvalues = $values{$table}{$id}; # Get values for t +able/ID if (defined($pvalues)) { push @$pout, @$pvalues; # Save values } else { push @$pout, ( "n.a." ) x $ncols; # Missing value = +N/A } } } # Verify results print "=== Verify results ===\n"; foreach my $id (@ids) { my $pvalues = $output{$id}; printf "%12.12s | %s\n", $id, join(" | ", @$pvalues); }

The output of which is:

gene 1 | value 1.1 | value 1.1 | value 1.2 gene 2 | value 2.1 | n.a. | n.a. gene 3 | n.a. | value 3.1 | value 3.2 gene 4 | value 4.1 | value 4.1 | value 4.2 gene 8 | value 8.1 | n.a. | n.a.

Of course, you can always add more tables to the master table $ptables.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Merging/Rearranging Tables by liverpole
in thread Merging/Rearranging Tables by homeveg

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.