There's not really any "correct" way. ...or maybe it's more accurate to say, there's no wrong way as long as it works and doesn't gobble all your memory in the process. ;)

Here is yet another way to do it:

use strict; use warnings; my @files = @ARGV; my %table; foreach my $filename (@files) { open my $infile, '<', $filename or die $!; while( <$infile> ) { chomp; next unless $_; my( $name, $val ) = split; push( @{$table{ $name }}, $val ); } } my @names = sort keys %table; my $output = join "\t", "NAME\t", @files; foreach my $entry ( @names ) { $output .= join "\t\t", "\n$entry", @{$table{$entry}}; } $output .= map { local $" = "\t\t"; "@{$table{$_}}\n"; } @names; print $output, "\n";

Usage: perl scriptname filename1 filename2

It doesn't bother to check to ensure that all files contain the proper number of entries. That could cause your columns to become misaligned, if for example, the second file doesn't contain an entry for Georgia.


Dave


In reply to Re: Is this possible by davido
in thread Is this possible by Anonymous Monk

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.