in reply to How to differentiate an empty array from an unitialized one?
Three examples should suffice. (Note that in each case these are Perl one-liners intended for the command-line, so $ characters are escaped for the shell, thusly: \$. Only $ is seen by Perl.)
Without LIMIT, split omits trailing empty groups:
$ perl -e "use strict; use warnings; my \$a='a:b::c:d::'; my @b = spli +t(/\:/, \$a); use Data::Dumper; print Data::Dumper->Dump([\$a, \@b],[ +'a','b']);" $a = 'a:b::c:d::'; $b = [ 'a', 'b', '', 'c', 'd' ];
LIMIT=-1 causes them to be included:
$ perl -e "use strict; use warnings; my \$a='a:b::c:d::'; my @b = spli +t(/\:/, \$a, -1); use Data::Dumper; print Data::Dumper->Dump([\$a, \@ +b],['a','b']);" $a = 'a:b::c:d::'; $b = [ 'a', 'b', '', 'c', 'd', '', '' ];
Perl-5 sees adjacent commas in a list-constructor as nothing, so it produces a smaller-than-expected list which join then processes, interpreting undef as an empty string:
$ perl -e "my @a=('a','q',,undef,,'b','c',,); my \$b = join(':', @a); +use Data::Dumper; print Data::Dumper->Dump([\@a, \$b], ['a','b']);"Q $a = [ 'a', 'q', undef, 'b', 'c' ]; $b = 'a:q::b:c';
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to differentiate an empty array from an unitialized one?
by Marshall (Canon) on Jul 09, 2018 at 18:14 UTC | |
|
Re^2: How to differentiate an empty array from an unitialized one?
by AnomalousMonk (Archbishop) on Jul 09, 2018 at 20:18 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |