Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Puzzled by array

by kiat (Vicar)
on Jul 06, 2005 at 17:03 UTC ( #472881=note: print w/replies, xml ) Need Help??


in reply to Re: Puzzled by array
in thread Puzzled by array

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?

Replies are listed 'Best First'.
Re^3: Puzzled by array
by trammell (Priest) on Jul 06, 2005 at 17:10 UTC
    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!

      Ah thanks :) But please bear with this nagging question: why doesn't \@letters point to the changed @letters inside the loop?

      Update

      Maybe I understand now...see modified code below:

      use strict; my $some_word = 'race'; my @letters = split //, $some_word; my (@array1, @array2); # Added this to show that the same $letter_ref is pushed into @array2 my $letters_ref = \@letters; for (1..@letters) { my @new = @letters; push (@array1, \@new); push (@array2, $letters_ref); my $myshift = shift @letters; push(@letters, $myshift); }
      Update 2

      My above reasoning was wrong. See my reply to neniro at Re^2: Puzzled by array

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://472881]
help
Chatterbox?
[holli]: you know, monk days are fun and all. but too many of them are bad for your health
[1nickt]: ...modern car. Suggests using TLA+ or other software modeling language to autogenerate code.
1nickt is not sure more and more layers separating the programmer from the program is the right way to go...
[holli]: happy one, shmem ;)
[1nickt]: I had an interview couople of weeks ago, heard from a principal engineer that a friend of his who works at The Google told him that the self-teaching software that determines the dynamic pricing for their adverts...
[1nickt]: ... has become so complex that there is no one at the Goole who knows what it does.
[marto]: William Gibson was right
[moritz]: a classical problem with machine learning
[1nickt]: moritz isn;t the difference today that the machine learning algos are in charge of most stuffs?
[moritz]: depends on what you mean by "most stuff"

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2017-11-21 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (301 votes). Check out past polls.

    Notices?