in reply to How to extract the number of cols from a matrix?

If the rows of a matrix are well-constructed, each will have the same number of elements, even if some elements are undefined. my $cols = @{$mat[0]}; To check for that goodness,

@mat == grep { @{$_} == @{$mat[0]}; } @mat or die '@mat not well-formed';
The equality tests put all those arrays in scalar context, so their counts are what is tested.

See tye's References quick reference for a concise key to understanding reference manipulations.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: How to extract the number of cols from a matrix?
by ikegami (Patriarch) on Jun 26, 2005 at 14:04 UTC

    Instead of counting the well-formed rows when trying to determine the existence of malformed rows, I'd count the malformed ones. In effect, you're using double negation ("no not well-formed rows"), which is less clear than the straightforward approach.

    die '@mat malformed' if grep { @{$_} != @{$mat[0]} } @mat;