in reply to Re: assigning to array from a split with some empty fields
in thread assigning to array from a split with some empty fields
$text = 'a|b|c|||||||||e||||'; @array = split(/\|/,$text); print join '-', @array;
can become ...
$text = 'a|b|c|||||||||e||||'; # our input might have holes in it! $text =~ s#\|\|#\|NULL\|#g; # make holes read as 'NULL' @array = map { $_ eq 'NULL' ? undef : $_ } split(/\|/,$text); print join '-', @array;
Code is of course untested, but you see the general madness of it. We're filling up a hole with something, digging up the hole, and then getting the hole back just like we left it :) Ok I just like overusing map. If you have a map, everything looks like a nail. Good thing Magellan didn't have a map.
|
|---|