Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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
Replies are listed 'Best First'.
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 lurking in the Monastery: (6)
As of 2015-08-01 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls