SPUG:Split question

Michael R. Wolf MichaelRunningWolf at att.net
Fri Mar 21 23:07:26 CST 2003


"Pommert, Daniel" <Daniel.Pommert at VerizonWireless.com> writes:

> You need to provide the third parameter to split and it needs to
> either be a very large number or a negative number.  Otherwise, as
> per the documentation, split will intentionally strip off the
> trailing empty fields.

This bit me years ago before I recognized the importance of that one
sentence.  When I teach it now, I tell folks to *always* put a -1 as
the 3rd parameter if they're splitting *data* because trailing
delimiters are delimiting a null field.  This is different from
splitting *text*, in which trailing delimeters are supurflous, and
should be disposed of.

    # root:q.mJzTnu8icF.:0:10:superuser:/:/bin/csh
    # xxx::100:100:mb_daemon:/usr/make-believe/daemon:
    my $line = qx(head /etc/passwd);
    my ($id, @other_fields, $shell) = split /,/, $line, -1;
                
Some accounts contain a null 7th field.  Without the 3rd argument,
$shell will always contain the last *filled* argument, not the *7th*
field.  Said differently, @other_fields will at most 5 fields, but
occasionally less.

-- 
Michael R. Wolf
    All mammals learn by playing!
        MichaelRunningWolf at att.net




More information about the spug-list mailing list