http://qs321.pair.com?node_id=710104


in reply to Re: Avoiding vivification with array slice
in thread Avoiding vivification with array slice

Cool. Accessing $a[$i] isn't enough to vivify (otherwise your defined test would do it).

So we don't actually need the defined test in your map and can get away with:

# This *doesn't* vivify $a[3,4,5] @b = map { $a[$_] } (0..5);

Thanks.

Replies are listed 'Best First'.
Re^3: Avoiding vivification with array slice
by ikegami (Patriarch) on Sep 09, 2008 at 19:45 UTC
    The point of the defined wasn't to prevent autovivification, but to implement the grep you had in your original code. The following two snippets are equivalent.
    @b = map { defined($a[$_]) ? ($a[$_]) : () } 0..5;
    @b = grep { defined } map { $a[$_] } 0..5;

    What you suggested (@b = map { $a[$_] } 0..5;) is *not* equivalent.