#!/usr/bin/perl -w
use strict;
use Data::Dumper;
$|++;
my @AoA = ( undef,
[1, 2],
undef,
[3, 4]);
my @flat = map{defined (@$_) ? @$_ : ()}@AoA;
print join (",",@flat),"\n"; # prints: 1,2,3,4
my $AoAref = \@AoA;
@flat = map{ defined (@$_) ? @$_ : () }@$AoAref;
print join (",",@flat),"\n"; # prints: 1,2,3,4
=so far prints:...
1,2,3,4
1,2,3,4
=cut
@AoA = ( undef,
[1, undef],
undef,
[3, 4]);
$AoAref = \@AoA;
# "undef" is a legal Perl value.
# to handle this idea of an undef value within
# an defined array (undef is a legal Perl value)! Yes, use grep.
#
# my map filters out "undefined references to arrays"
# within an AoA, I would use grep to filter
# that undesired "undef" value out if what is meant
# a value within in an array.
# as shown before: grep{} map{}.
My code is just a special case.
The more general case is to flatten the whole thing out and grep out the undefined values provided that if what is meant by "sparse" is that not only entire rows are missing but also columns within defined rows. "undef" is a legal Perl value and requires some kind of "grep" to "get rid of it"
|