Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^2: printing contents of small files into one larger one

by virtualsue (Vicar)
on Nov 09, 2005 at 13:59 UTC ( #507074=note: print w/replies, xml ) Need Help??

in reply to Re: printing contents of small files into one larger one
in thread printing contents of small files into one larger one

That's cute, but it doesn't quite do what was asked. It prints each file's entries per output row, when what's needed is each file per output column. Unfortunately that transposition greatly complicates the task. Either you need to read all the files into memory in some data structure, or open all the files and for every line that is to be output, read one entry per filehandle (see holli's answer). For the sake of completeness, here's a way to read all the data into a "hash of arrays" data structure. The keys are the names of the data files, and the values are references to the data read from each data file.

If the only goal here is to print out the data, I would do it holli's way. Reading all the files into memory into one big data structure wouldn't scale very well.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @files = glob('data/*'); my %filedata; for my $file (@files) { open my $fh, "<$file" or die "Ack, Can't open $file"; chomp( my @data = <$fh> ); close $fh; $filedata{$file} = \@data; } print Dumper(\%filedata);
$VAR1 = { 'test/bb' => [ '5.6', '5.7', '5.9' ], 'test/aa' => [ '1.3', '1.4', '1.5' ] };

Replies are listed 'Best First'.
Re^3: printing contents of small files into one larger one
by inman (Curate) on Nov 09, 2005 at 17:12 UTC
    Fair enough. I should have read the question!

    I have modified the technique to build an AoA in memory before writing it out. The OP wasn't clear on how files of differing length were to be treated. The code below expands the AoA as necessary to cope with files of different lengths.

    my @lines; my $line = 0; my $filecount = 0; while (<>) { chomp (${$lines[$line++]}[$filecount] = $_); if (eof) { $line = 0; $filecount++; } } foreach (@lines) { $#{$_} = $filecount -1; print join("\t", @{$_}), "\n" ; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://507074]
and a moth chases the moon...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2018-04-25 00:04 GMT
Find Nodes?
    Voting Booth?