Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

The trick here is to use a hash of arrays to store the table entries. Consider:

use warnings; use strict; my $table1 = <<TABLE; ID value Aa 22 Bb 28 Cc 32 Dd 50 TABLE my $table2 = <<TABLE; ID value Aa 34 Cc 112 Dd 77 Ee 89 Kk 124 TABLE my $table3 = <<TABLE; ID value Bb 75 Cc 91 Dd 132 TABLE my $table4 = <<TABLE; ID value Aa 66 Cc 94 Ee 213 Gg 250 TABLE my %values; my $tableIndex = 0; for my $inFileVar (\$table1, \$table2, \$table3, \$table4) { open my $inFile, '<', $inFileVar or die "Can't open $inFileVar: $! +\n"; while (<$inFile>) { chomp; my ($id, $value) = split; next if $id eq 'ID'; $values{$id}[$tableIndex] = $value; } ++$tableIndex; } for my $id (sort keys %values) { print "$id"; print ' ', $values{$id}[$_] || 0 for 0 .. $tableIndex - 1; print "\n"; }

Prints:

Aa 22 34 0 66 Bb 28 0 75 0 Cc 32 112 91 94 Dd 50 77 132 0 Ee 0 89 0 213 Gg 0 0 0 250 Kk 0 124 0 0

Note that the variables in the first for loop are used as in memory files so the open provides a file handle that uses the contents of each variable as the file contents. This technique is handy for avoiding extra files in sample code. Your real code can simply substitute a list of file names for the list of variable references.

True laziness is hard work

In reply to Re: I need help joining tab-delimited files/tables! by GrandFather
in thread I need help joining tab-delimited files/tables! by nabiana

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2014-09-20 19:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (160 votes), past polls