G'day PilotinControl,

Firstly, I think you need to step back from the question of one or two loops and consider what data you're looping over.

Currently, you're opening a file, reading all its data into an array, closing the file, then reading all the same data again from the array. Beyond the waste of CPU cycles to read the data twice, you've now copied all the data from disk into memory: with a large file this could be problematic.

A better approach is to read and process each line from the file without ever creating an intermediate array. Here's one way to do it.

#!/usr/bin/env perl -l use strict; use warnings; my $format = "| %-15s | %-15s |\n"; my $format_width = 37; print '=' x $format_width; printf $format => 'Inbound Track 1', 'Inbound Track 2'; print '-' x $format_width; my ($file1, $file2) = qw{pm_1126327_inbndtrk1.txt pm_1126327_inbndtrk2 +.txt}; open my $fh1, '<', $file1 or die "Can't open '$file1' for reading: $!" +; open my $fh2, '<', $file2 or die "Can't open '$file2' for reading: $!" +; while (<$fh1>) { printf $format => get_data($_), get_data(scalar <$fh2>); } close $fh1; close $fh2; print '=' x $format_width; sub get_data { chomp(my $line = shift); join ' ' => (split /:/ => $line)[1,2]; }

Output:

$ pm_1126327_combine_file_data.pl ===================================== | Inbound Track 1 | Inbound Track 2 | ------------------------------------- | B&O 101 | CSXT 1001 | | B&O 102 | CSXT 1002 | =====================================

Notes:

The data you show is ideal, in that both files are the same length, all records have data, no data is corrupt. Real world data can rarely be relied upon to be ideal. In a while (1) {...} loop, you can code whatever loop exit conditions you want. In the following example, I've simply checked for an end-of-file (eof) condition in one file; in your real-world code, you'd probably want a lot more checking than this.

while (1) { last if eof $fh1; printf $format => get_data(scalar <$fh1>), get_data(scalar <$fh2>) +; }

You can replace the while loop in my first script with this one and get exactly the same output.

Update: I had three instances of the literal '37' hard-coded. This is not good! What the hell does '37' refer to? You'll now find $format_width instead of '37': much better.

-- Ken


In reply to Re: Two Column Data by kcott
in thread Two Column Data by PilotinControl

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.