Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Avoiding vivification with array slice

by jbert (Priest)
on Sep 09, 2008 at 14:45 UTC ( #710104=note: print w/ replies, xml ) Need Help??


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.


Comment on Re^2: Avoiding vivification with array slice
Select or Download Code
Re^3: Avoiding vivification with array slice
by ikegami (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://710104]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-09-02 03:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (19 votes), past polls