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.
In reply to Re^3: split/map weirdness: empty strings vs undef
by Aristotle
in thread split/map weirdness: empty strings vs undef
by hossman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |