Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Re: combine multiple files into one (line by line)

by extremely (Priest)
on Apr 05, 2001 at 23:36 UTC ( #70245=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: combine multiple files into one (line by line)
in thread combine multiple files into one (line by line)

I don't think he was intending to load the entire file into memory. The trick is to keep an array of open FILEHANDLES and read one line at a time from each...

#!/usr/bin/perl -w use strict; use IO::File; #interleave files, first argument is line count, rest are files. my $lc = shift; my @fhs; foreach (@ARGV) { my $fh = new IO::File; open ($fh, "<$_"); push @fhs, $fh; } while (--$lc) { foreach (@fhs) { print scalar(<$_>); } }

Tested minimally but the path is clear from here I hope.

--
$you = new YOU;
honk() if $you->love(perl)


Comment on Re: Re: Re: combine multiple files into one (line by line)
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://70245]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2015-07-03 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls