String.prototype.split fixed fields extension

Roger Andrews roger.andrews at mail104.co.uk
Fri Mar 23 14:36:56 PDT 2012


String.prototype.split is good for cutting records into fields based on a
delimiter string or regexp.  E.g.
        rec.split( ',' )           // split CSV record (no commas in fields)
        rec.split( /\s+/ )     // split into whitespace-separated fields

How about extending 'split', or inventing a new method 'splitlen', which
splits a record into defined-length fields?  This simplifies a long list of
'substring's.


Old data formats invented in the days of punch-cards are still around.  For
example NASA's two-line element set
(http://en.wikipedia.org/wiki/Two-line_element_set)
which records the orbital elements of Earth satellites.
E.g. here is the TLE for for International Space Station:
   ISS (ZARYA)
   1 25544U 98067A   08264.51782528 -.00002182  00000-0 -11606-4 0  2927
   2 25544  51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537

Proposed design:
     split( len1, len2, len3, len4, ..... )       // returns array of fields
where each numeric length argument either
   (1) captures a field of the given length if positive, or
   (2) ignores a field of the absolute given length if negative.
The special argument "*" could repeat the previous argument to the end of
the record.

Examples:
   // chop into 5-char fields
   rec.split( 5, "*" )
   // capture a 1-char and a 5-char field and all chars after index 17
   rec.split( -7, 1, 5, -4, Infinity )

 



More information about the es-discuss mailing list