Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Opening multiple files

by Eliya (Vicar)
on Jan 29, 2012 at 21:41 UTC ( #950663=note: print w/replies, xml ) Need Help??

in reply to Opening multiple files

You could do something like this:

#!/usr/bin/perl -w use strict; my @fh; # open all files for my $i (0..$#ARGV) { open $fh[$i], "<", $ARGV[$i] or die $!; } while (1) { my @lines; # read one line from each file push @lines, scalar readline $fh[$_] for 0..$#ARGV; last unless defined $lines[0]; chomp @lines; print join(",", @lines), "\n"; }
$ ./ infile1 infile2 infile3 ... > outfile

(Note that you have to use readline with "complex" file handle expressions like $fh[$i], as <$fh[$i]> wouldn't work here.)

Replies are listed 'Best First'.
Re^2: Opening multiple files
by kielstirling (Scribe) on Jan 30, 2012 at 01:45 UTC

    Maybe I'm still asleep but, will this ever return out of the while loop?

    -Kiel R Stirling
      will this ever return out of the while loop?

      Yes, due to the

      last unless defined $lines[0];

      where $lines[0] becomes undef when EOF of the first file has been reached.

        arr yes that will do it .. so i am still asleep :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950663]
[choroba]: BTW, both the bands I play in performed on Friday (at the same event). Quite an exhausting experience.
[Corion]: choroba: Ooof! I can imagine that ;)
[Corion]: I think at a Perl workshop some years ago I gave two talks on the same day and after that decided to only submit one talk per event ;)
[LanX]: you play the triangle, right?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-03-27 11:39 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (319 votes). Check out past polls.