in reply to Collapsing Arrays with Empty Values

An empty list, in list context, is just that -- a list with 0 elements. In scalar context, though, something must be assigned, and undef is that assigned value. Indexing beyond the end of a list, in list context, returns an empty list, and undef in scalar context.

It is a possible oddity, though, that indexing an element of a list which is undef and assigning it to a list returns an empty list, and not the undef that was there -- this is probably because it was within the same blanket solution to the problem above. However, in 5.6.0, this buggish behavior was removed.
# in 5.005_02 $x = (1,2,3)[10]; # $x is undef @x = (1,2,3)[10]; # @x is (), not (undef) @x = (undef)[0]; # @x is (), not (undef) # in 5.6.0 $x = (1,2,3)[10]; # $x is undef @x = (1,2,3)[10]; # @x is (), not (undef) @x = (undef)[0]; # @x is (undef), not ()
My golf approach would be:
# returns the collapsed list: 15 chars # 123456789_12345 sub collapse {grep{defined}@_} # returns the collapsed list, via array ref: 20 chars # 123456789_123456789_ sub collapse {grep{defined}@{+pop}} # modifies array ref elements in place: 26 chars # 123456789_123456789_123456 sub collapse {@$_=grep{defined}@{$_=pop}}


japhy -- Perl and Regex Hacker