use strict; use warnings; use Test::More tests => 2; my $have = '1,,3,4,,,'; my @want = (1, undef, 3, 4, undef, undef, undef); my @try = map { length ($_) ? $_ : undef } split (',', $have, -1); is_deeply \@try, \@want, 'Array is correct'; # Your criterion my $count = 0; foreach (@try) { ++$count; } is $count, 7, 'Count is correct';