note
Kenosis
<p>Your reply makes sense, sauoq. I see that I assumed too much by the OP's field representations as not containing any spaces, so it would be best to split on the known field delimiter. Indeed, it would be disastrous if the first field contained spaces. Good catch and thank you for bringing this to my attention.</p>
<p>Update:</p>
<p>I was curious to see whether there was any speed difference between <c>split</c>ing all or <c>split</c>ing some columns, so I ran the following which creates and <c>split</c>s a 20 column x 10000 row file:</p>
<c>
use Modern::Perl;
use Benchmark qw(cmpthese);
my $entry = "aaaaaaaaaaaaaaaaa";
my $columnsFile = 'columns.txt';
open my $file, ">$columnsFile" or die $!;
do { print $file "$entry\t" x 19; say $file $entry }
for 1 .. 10000;
close $file;
sub splitAll {
open my $file, "<$columnsFile" or die $!;
while (<$file>) {
my @columns = split /\t/;
}
close $file;
}
sub splitSome {
open my $file, "<$columnsFile" or die $!;
while (<$file>) {
my @columns = ( split /\t/ )[ 1 .. 2 ];
}
close $file;
}
cmpthese(
-5,
{
splitAll => sub { splitAll() },
splitSome => sub { splitSome() }
}
);
</c>
<p>Results:</p>
<c>
Rate splitAll splitSome
splitAll 19.8/s -- -21%
splitSome 25.1/s 27% --
</c>
<p>In this case, <c>split</c>ing only some shows a significant speed advantage--and with this relatively small file. I ran the script many times, getting as high as 31% for <c>splitSome</c> and as low as 21%--but always showing that <c>splitSome</c> is significantly faster.</p>
974153
974170