in reply to Two Column Data

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

Replies are listed 'Best First'.
Re^2: Two Column Data
by PilotinControl (Pilgrim) on May 12, 2015 at 00:14 UTC

    Thank you Ken! I was looking for a while loop to use as another instance I am reading data from several files and displaying the information in one column. This is the first time at my attempt of reading several files and putting the data into two separate columns and trying to figure out how to format the data. This is a very good working example that I can now build off of for future sub routines. Thanks again!