Perl Programming/Keywords/split

Previous: splice Keywords Next: sprintf

The split keyword

edit

split splits the EXPRESSION in a list of strings and returns this list. Without EXPRESSION, $_ is used as source. Anything in EXPRESSION that matches PATTERN is taken to be a separator, which may be longer than one character or have even zero length (results in a zero-width match). When separating the EXPRESSION, the separator itself is not added to the strings.

Syntax

edit
  split /PATTERN/, EXPRESSION, LIMIT
  split /PATTERN/, EXPRESSION
  split /PATTERN/
  split

Examples

edit
  The print
  $login = "anton";
  $passwd = "j4\/D\\S";
  $uid = 1721169842;
  $gid = 8421691721;
  $gcos = "nothing";
  $home = "\\";
  $shell = "ksh";

  $rec = join(':', $login, $passwd, $uid, $gid, $gcos, $home, $shell);

  print $rec, "\n", join(':', split('8', $rec)), "\n";
split returns the colon-separated string elements, which take '8' also as a separator:
anton:j4/D\S:1721169842:8421691721:nothing:\:ksh
anton:j4/D\S:1721169:42::421691721:nothing:\:ksh

See also

edit
Previous: splice Keywords Next: sprintf