Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Puzzled by array

by kiat (Vicar)
on Jul 06, 2005 at 16:43 UTC ( #472870=perlquestion: print w/ replies, xml ) Need Help??
kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm puzzled by the following code:

use strict; my $some_word = 'race'; my @letters = split //, $some_word; my (@array1, @array2); for (1..@letters) { my @new = @letters; push (@array1, \@new); push (@array2, \@letters); my $myshift = shift @letters; push(@letters, $myshift); } print "array1 contains:\n"; foreach my $aref (@array1) { print "@$aref\n"; } print "\n\n"; print "array2 contains:\n"; foreach my $aref (@array2) { print "@$aref\n"; }
The output is as follows:

array1 contains: r a c e a c e r c e r a e r a c array2 contains: r a c e r a c e r a c e r a c e
I was expecting array2 to contain the same elements as array1 but that's not the case. Can someone explain why?

Thanks in anticipation.

Comment on Puzzled by array
Select or Download Code
Re: Puzzled by array
by revdiablo (Prior) on Jul 06, 2005 at 16:51 UTC

    First you make a copy of @letters:

    my @new = @letters;

    Then you change @letters directly:

    my $myshift = shift @letters; push(@letters, $myshift);

    But you never change the copy. Why would you expect it to be changed?

    Update: ugh, Tanktalus is right. I didn't read the code carefully enough. Please read his reply instead. :-)

Re: Puzzled by array
by Tanktalus (Canon) on Jul 06, 2005 at 16:53 UTC

    It's because @array2 is an array of references - all to the same @letters array. @array1 is an array of references - but each element in @array1 is a separate reference to a separate array - that array is @new which is created each time through the loop.

    As you modify @letters, @array2's members are changed because they are the same. But @new isn't modified again after it's assigned into the @array1 list, so you're getting a snapshot of each point through the list.

    Unlike C, in perl when you create a new variable with "my", you actually get a new variable, unique from all similarly named variables that came before it, and unique from all dissimilarly named variables that came before it. You can take a reference to it, and that variable will not disappear until all references to it go away as well. After going through the loop, all you're left with is anonymous references to all the old @new's.

    Hope that helps,

    Update: Note to revdiablo - on original reading of the code, I actually expected @array2 to be the one changed, and not @array1. So maybe I understand the confusion a bit ;-)

      Thanks Tanktalus :)

      As you modify @letters, @array2's members are changed because they are the same.
      Should are be aren't?
Re: Puzzled by array
by trammell (Priest) on Jul 06, 2005 at 16:57 UTC
    Because you're pushing the same arrayref into @array2 repeatedly. You can see this by changing your code:
    foreach my $aref (@array2) { print "$aref => @$aref\n"; }
      Thanks trammell :)

      But isn't @letters modified in the loop? Because when I have @new = @letters, @new stores the changed seqences.

      Does that mean that \@letters always point to the original @letters?

        Sure, \@letters doesn't change. But when you leave the loop, @letters has been set back to r a c e--it looks like it hasn't changed, but it's really just been looped back to where it started.

        A few more print()'s would make this clear...

        Update: Fixed a typo, plus here's a modified version that makes it clearer what's happening (to me, anyhow):

        #!perl -l use strict; use warnings; my $some_word = 'race'; my @letters = split //, $some_word; my (@array1, @array2); for (1 .. @letters) { my @new = @letters; push (@array1, \@new); push (@array2, \@letters); my $myshift = shift @letters; push(@letters, $myshift); print "$_ : @letters : $array1[-1] : $array2[-1]" } print ''; print "array1 contains:"; foreach my $aref (@array1) { print "$aref => @$aref"; } print ''; print "array2 contains:"; foreach my $aref (@array2) { print "$aref => @$aref"; } __END__ 1 : a c e r : ARRAY(0x8067740) : ARRAY(0x805f380) 2 : c e r a : ARRAY(0x807c91c) : ARRAY(0x805f380) 3 : e r a c : ARRAY(0x807c8c8) : ARRAY(0x805f380) 4 : r a c e : ARRAY(0x807c820) : ARRAY(0x805f380) array1 contains: ARRAY(0x8067740) => r a c e ARRAY(0x807c91c) => a c e r ARRAY(0x807c8c8) => c e r a ARRAY(0x807c820) => e r a c array2 contains: ARRAY(0x805f380) => r a c e ARRAY(0x805f380) => r a c e ARRAY(0x805f380) => r a c e ARRAY(0x805f380) => r a c e

        Update 2: kiat asks: (W)hy doesn't \@letters point to the changed @letters inside the loop? That's just it--it does!

Re: Puzzled by array
by neniro (Priest) on Jul 06, 2005 at 18:01 UTC
    It's easier to see that the array @letters is fully rotated if you change the loop to: for (1..@letters-1) { so you can see the last step before full rotation.
      Thanks, neniro!

      That and rammell's update 2 helped. As I was brushing my teeth, it suddenly hit that I was having something like

      (last_changed_array_of_letters last_changed_array_of_letters last_changed_array_of_letters last_changed_array_of_letters)

      in @array2.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-09-15 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (146 votes), past polls