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

Re: Re: Re: Non-destructive array processing

by pdcawley (Hermit)
on Jan 20, 2003 at 22:33 UTC ( #228512=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Non-destructive array processing
in thread Non-destructive array processing

Heh. I'm so used to slinging objects around rather than simple scalars I just took it as read that it would be a shallow copy. Note that, in the original case that's not a problem because assigning to @chunk makes a copy of the value.


Comment on Re: Re: Re: Non-destructive array processing
Download Code
Replies are listed 'Best First'.
Re: Re: Re: Re: Non-destructive array processing
by Ovid (Cardinal) on Jan 20, 2003 at 22:54 UTC

    Whoa! You're right. That doesn't seem to be very DWIM. The following snippet does not alter the elements of @array.

    my @array = 1..10; my $r1 = sub { \@_ }->(@array); while (my @chunk = splice @$r1, 0, 2) { print "Chunk: @chunk\n"; } $_++ foreach @$r1; print "Original array is still intact! (@array)\n";

    However, by moving the auto-increment line above the while loop:

    my @array = 1..10; my $r1 = sub { \@_ }->(@array); $_++ foreach @$r1; while (my @chunk = splice @$r1, 0, 2) { print "Chunk: @chunk\n"; } print "Original array is not intact! (@array)\n";

    This looks like some weird "copy on write" behavior that I was not aware of. Is this new, or is it Yet Another Feature that I didn't know about? :)

    Update: Okay, I see that adrianh answered the question.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2015-07-28 23:14 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 (260 votes), past polls