My first question would be how many input files you have. If it's not more than the number of files you can open at once, then I'd do this (pseudocode):

open all input files, making array of file descriptors open output file for writing while the first input file has remaining lines for each input file descriptor read one line get desired column write to output file write newline to output file close all files

If your input files have different numbers of lines, you may need a bit more to handle that. But basically, open all files and process the first line of each input file, creating the first line of the output file. Then move on to the second lines, then the third, etc.

If there are more files than you can have open simultaneously, you'll have to do something else. Yes, you can repeatedly add columns to the end of your output file, preferably using something like Text::CSV to keep things correct, but that would be pretty inefficient, so don't do that if you don't have to. Honestly, I'd probably do this with shell tools, which are pretty handy for things like breaking lines on a delimiter (when you don't have to worry about things like quoted delimiters within fields) and handling multiple columns of text:

#!/bin/sh # args: $1 - directory holding input files # $2 - column to save # $3 - output file # temporary files created in /tmp/columns n=0 for i in $1/*; do n=`expr $n + 1` echo $i >/tmp/columns/$n cut -f $2 $i >>/tmp/columns/$n done paste /tmp/columns/* >$3 rm /tmp/columns/*

Aaron B.
Available for small or large Perl jobs; see my home node.


In reply to Re: Adding columns in a loop to an existing file using Perl by aaron_baugher
in thread Adding columns in a loop to an existing file using Perl by nanoplasmonic

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.