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'
]
};
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.