in reply to Optimizing Splitting of Array

Hi,

there is a 'limit' arg to split (see perldoc -fsplit) this could do what you want:

use warnings; use strict; use Data::Dumper; my @rows; while (<DATA>) { my @data = split(/ /,$_, 6); push (@rows, \@data); } print Dumper \@rows; __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
Cheers, Christoph

Replies are listed 'Best First'.
Re^2: Optimizing Splitting of Array
by Marshall (Canon) on Jun 09, 2009 at 05:31 UTC
    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).
Re^2: Optimizing Splitting of Array
by bichonfrise74 (Vicar) on Jun 08, 2009 at 21:39 UTC
    Thanks!