Understanding this card behavior

I am using a map to retrieve the first element of a 2D array. Here is a small piece of code.

my $array = [ [1,11,111], [2,22], undef, [4] ];

my @firstList = map { (defined $_) && $_->[0] } @$array;

So, I expect the map to return an array containing elements with either undef or the first element of the $ array element.

but the result is not what I expect. For undef, I get an element of type "scalar".

If I change the map operator with the next block, I get the expected result.

my @firstList = map { $_->[0] } @$array;

Please help me sort out these two statements of the map.

+5
source share
2 answers

Both of them return the result of the last operation performed.

, (defined $_) && $_->[0] undef, , defined $_ . $_->[0] . defined $_ , false, , , 0.

@$array, undef.

+4
map { (defined $_) && $_->[0] }

. : defined($_) && $_->[0] , defined(undef) && $->[0] ( '', ..), - 1 && $->[0], $- > [0].

, , , .

0

All Articles