Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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"; }


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

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (4)
    As of 2018-05-26 22:41 GMT
    Find Nodes?
      Voting Booth?