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).

In reply to Re^2: Optimizing Splitting of Array by Marshall
in thread Optimizing Splitting of Array by bichonfrise74

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.