The use of limit on the split is a great idea here! One suggestion concerns the use of split/[space]/: usually splitting on a single space is not what is needed (could be, but not often). What happens here is that abc has 2 spaces following it and this results in an extra null token in the output list (@data). This caused by the 2nd space after abc. Usually split(/\s+/,$_,6) would work out better (btw: default split() splits on \s+).
Another technique is the use of list slice. There can be some good reasons to combine this with split limit. The below code shows how to "get rid of a value" from the split. In this case, the date token. You probably don't want to do that, but this is just an example.
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my @rows;
while (<DATA>)
{
my @data = (split(/\s+/,$_, 6))[0,1,3..5];
push (@rows, \@data);
}
foreach (@rows)
{
print "@$_";
}
#prints
#abc 322 aaa aadda dasdas a1 a2 a3
#def 433 dasd bdbdbd wings b1 b2 b3 b4 b5
__DATA__
abc 322 2/3/09 aaa aadda dasdas a1 a2 a3
def 433 3/4/08 dasd bdbdbd wings b1 b2 b3 b4 b5
As another point about "space splitting": If you split a complete line with \s+, the ending \n will be removed because the \s set includes \n\f\r\space\t (so when splitting a complete line with \s, you do not have to "chomp" it first).
|