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.
Update:
I was curious to see whether there was any speed difference between spliting all or spliting some columns, so I ran the following which creates and splits a 20 column x 10000 row file:
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() }
}
);
Results:
Rate splitAll splitSome
splitAll 19.8/s -- -21%
splitSome 25.1/s 27% --
In this case, spliting 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 splitSome and as low as 21%--but always showing that splitSome is significantly faster.
-
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.