in reply to Re: Re: split/map weirdness: empty strings vs undef
in thread split/map weirdness: empty strings vs undef
This is also documented in perldoc -f split. Of course now every other element is the separator, which isn't what we wanted. So out with them:$ perl -le'print join " : ", split /(\|)/, "2|3|||||"' 2 : | : 3 : | : : | : : | : : | : : |
There you go.$ perl -le'print join " : ", grep ++$i%2, split /(\|)/, "2|3|||||"' 2 : 3 : : : :
Update: Yikes! I caught a mistake: the last field still disappears if empty. Count the pipes in the string and the colons in the output.. At this point we lose some grace.. The oneliner is ugly:
while the multiliner is awkward:$ perl -le'print(join(" : ", grep(++$i%2, split /(\|)/, "2|3|||||"), ( +"")x(1-$i%2)))' 2 : 3 : : : : :
Oh well.$ perl -le'my @field = grep ++$i%2, split /(\|)/, "2|3|||||"; push @fi +eld, "" unless $i%2; print @field' 2 : 3 : : : : :
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^3: split/map weirdness: empty strings vs undef
by hossman (Prior) on Oct 04, 2002 at 22:34 UTC | |
by Aristotle (Chancellor) on Oct 05, 2002 at 18:01 UTC |