in reply to undef vs empty string '' from split
If you hadn't specified the limit, you would have got what you expected:
Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.)(from split, emphasis mine). Note that using a limit of 1 does not split the string (which was news to me):...
If LIMIT is unspecified or zero, trailing null fields are stripped (which potential users of "pop" would do well to remember).
update: output:#!/usr/bin/perl -w use strict; sub pr { print "'$_[0]' (no limit) => [".join(",",map { defined $_ ? "'$_'" + : 'undef' } split /=/,$_[0])."]\n" ; print "'$_[0]' (limit 1) => [".join(",",map { defined $_ ? "'$_'" +: 'undef' } split /=/,$_[0],1)."]\n" ; print "'$_[0]' (limit 2) => [".join(",",map { defined $_ ? "'$_'" +: 'undef' } split /=/,$_[0],2)."]\n" ; } pr(''); pr('a'); pr('='); pr('a='); pr('a=b');
updated again fixed code & output.'' (no limit) => [] '' (limit 1) => [] '' (limit 2) => [] 'a' (no limit) => ['a'] 'a' (limit 1) => ['a'] 'a' (limit 2) => ['a'] '=' (no limit) => [] '=' (limit 1) => ['='] '=' (limit 2) => ['',''] 'a=' (no limit) => ['a'] 'a=' (limit 1) => ['a='] 'a=' (limit 2) => ['a',''] 'a=b' (no limit) => ['a','b'] 'a=b' (limit 1) => ['a=b'] 'a=b' (limit 2) => ['a','b']
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: undef vs empty string '' from split
by chrism01 (Friar) on Jun 21, 2007 at 01:18 UTC | |
by Joost (Canon) on Jun 21, 2007 at 01:39 UTC | |
by Sidhekin (Priest) on Jun 21, 2007 at 05:32 UTC |