assignments of arrays

Discussion in 'Perl Misc' started by Toralf Förster, Feb 15, 2013.

  1. In the following program I'd expect that line 29 fills @Values with zeros, but for i==2 this is counterproofed.

    Why ?

    $ nl -ba zero.pl
    1 #!/usr/bin/perl
    2
    3 # Toralf Förster
    4 # Hamburg
    5 # Germany
    6
    7 use strict;
    8 use diagnostics;
    9 $diagnostics::pRETTY = 1;
    10
    11 use warnings FATAL => 'uninitialized';
    12 use Carp ();
    13 $SIG{__DIE__} = \&Carp::confess;
    14
    15
    16 my $Cols = 30;
    17 my $Rows = 16;
    18
    19 my @Zero = ();
    20 foreach my $r (0..$Rows+1) {
    21 foreach my $c (0..$Cols+1) {
    22 $Zero[$r][$c] = 0;
    23 }
    24 }
    25
    26 my $N = 10;
    27 foreach my $i (1..$N) {
    28
    29 my @Values = @Zero;
    30
    31 foreach my $r (0..$Rows+1) {
    32 foreach my $c (0..$Cols+1) {
    33 if ($Values[$r][$c] != 0) {
    34 die "i=$i\tr=$r\tc=$c\t$Values[$r][$c]\n";
    35 }
    36 }
    37 }
    38
    39 foreach my $r (0..$Rows+1) {
    40 foreach my $c (0..$Cols+1) {
    41 $Values[$r][$c] = 1;
    42 }
    43 }
    44 }
    45
    46
    47 exit (0);
    48


    tfoerste@n22 ~/workspace/misc $ ./zero.pl
    i=2 r=0 c=0 1
    at ./zero.pl line 34



    --
    MfG/Sincerely
    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
     
    Toralf Förster, Feb 15, 2013
    #1
    1. Advertising

  2. Be carefull Toralf Förster.
    At your statement my @Values = @Zero;
    you are "playing" with array references.
    So when you fill the @Values with 1 what you are doing is at the same time
    to fill the @Zero with 1
    what you must do , to have what you mean is the following:
    George Bouras
    Athens, Greece


    ....

    my $N = 10;
    foreach my $i (1..$N)
    {
    my @Values = ();

    for (my $i=0;$i<=$#Zero;$i++)
    {
    for (my $j=0;$j<=$#{$Zero[$i]};$j++)
    {
    $Values[$i][$j] = $Zero[$i][$j]
    }

    }

    foreach my $r (0..$Rows+1)
    {
    foreach my $c (0..$Cols+1)
    {
    print "i=$i\tr=$r\tc=$c\t$Values[$r][$c]\n";
    if ($Values[$r][$c] != 0) { die
    "i=$i\tr=$r\tc=$c\t$Values[$r][$c]\n"; }
    }


    }

    foreach my $r (0..$Rows+1) {
    foreach my $c (0..$Cols+1) {
    $Values[$r][$c] = 1 } }
    }

    exit 0;
     
    George Mpouras, Feb 15, 2013
    #2
    1. Advertising

  3. Henry, try to avoid the hardcoded values of $Rows and $Cols by using the
    array offsets !


    Ο "Henry Law" έγÏαψε στο μήνυμα
    news:...

    On 15/02/13 18:01, Toralf Förster wrote:
    > In the following program I'd expect that line 29 fills @Values with zeros,
    > but for i==2 this is counterproofed.
    > 19 my @Zero = ();
    > 20 foreach my $r (0..$Rows+1) {
    > 21 foreach my $c (0..$Cols+1) {
    > 22 $Zero[$r][$c] = 0;
    > 23 }
    > 24 }
     
    George Mpouras, Feb 15, 2013
    #3
  4. On 02/15/2013 08:07 PM, George Mpouras wrote:
    > you are "playing" with array references.
    > So when you fill the @Values with 1 what you are doing is at the same
    > time to fill the @Zero with 1


    ick

    @all
    thx for the explanation


    --
    MfG/Sincerely
    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
     
    Toralf Förster, Feb 15, 2013
    #4
  5. thanks nice
     
    George Mpouras, Feb 16, 2013
    #5
  6. Toralf Förster

    C.DeRykus Guest

    On Friday, February 15, 2013 2:57:16 PM UTC-8, Ben Morrow wrote:
    > Quoth "George Mpouras" <>:
    >
    > > ...

    >
    > An easier way to do this is to use the Clone module:
    >
    >
    >
    > use Clone "clone";
    >
    >
    >
    > my @Values = map clone($_), @Zero;
    >
    > # or
    >
    > my @Values = @{ clone \@Zero };
    >


    Alternatively: Storable's dclone:

    use Storable qw/dclone/;
    @Values = @{ dclone \@Zero };

    Storable is core which is a plus but I notice the docs mention:

    "Clone is faster for data structures with 3 or less levels,
    while dclone() can be faster for structures 4 or more levels deep."

    --
    Charles DeRykus
     
    C.DeRykus, Feb 16, 2013
    #6
  7. On 02/15/2013 08:07 PM, George Mpouras wrote:
    > At your statement my @Values = @Zero;
    > you are "playing" with array references.


    OTOH this was so clear for me b/c if I pass parameters to a sub like foo
    (@Values) then this means a call-by-value whereas foo (\@Values) passes
    just the reference to the sub called "foo".


    --
    MfG/Sincerely
    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
     
    Toralf Förster, Feb 17, 2013
    #7
  8. On 2013-02-17 08:16, Toralf Förster <> wrote:
    > On 02/15/2013 08:07 PM, George Mpouras wrote:
    >> At your statement my @Values = @Zero;
    >> you are "playing" with array references.

    >
    > OTOH this was so clear for me b/c if I pass parameters to a sub like foo
    > (@Values) then this means a call-by-value


    No.

    Parameters are passed by reference in Perl.

    For example, consider this code:

    sub foo {
    $_[1] = 5;
    }

    my @x = (1, 2, 3);
    foo(@x);
    print "@x\n";

    my ($x, $y, $z) = qw(a b c);
    foo($x, $y, $z);
    print "$x $y $z\n";
    __END__

    It prints
    1 5 3
    a 5 c
    which clearly shows that the array @x and the variably $y were passed by
    reference (in Perl jargon, we call this "aliasing", but it's the same
    concept).

    What creates the *illusion* that Perl function calls are by value is the
    convention to immediately assign parameters to local variables. So you
    would normally write foo as

    sub foo {
    my @p = @_;
    $p[1] = 5;
    }

    or

    sub foo {
    my ($p1, $p2, $p3) = @_;
    $p2 = 5;
    }

    Here the assignments in the second line alter only the local variable
    (@p or $p2, respectively), not the parameters. But it's the assignment
    in the first line which causes the values to be copied, not the function
    call.

    hp



    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
     
    Peter J. Holzer, Feb 17, 2013
    #8
  9. On 02/17/2013 03:41 PM, Peter J. Holzer wrote:
    >What creates the *illusion* that Perl function calls are by value is the
    >convention to immediately assign parameters to local variables. So you
    >would normally write foo as

    ....
    > Here the assignments in the second line alter only the local variable
    > (@p or $p2, respectively), not the parameters. But it's the assignment
    > in the first line which causes the values to be copied, not the function
    > call.
    >
    > hp


    ah - now I did understood it much better
    Thx for this answer

    --
    MfG/Sincerely
    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
     
    Toralf Förster, Feb 17, 2013
    #9
  10. Toralf Förster

    Guest

     
    , Jul 11, 2013
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    2
    Views:
    1,003
    Neil Zanella
    Oct 26, 2003
  2. Nicolas Matringe
    Replies:
    9
    Views:
    740
    Mike Treseler
    Jun 14, 2004
  3. Gary Thorpe

    Using aggregates for assignments

    Gary Thorpe, Jun 14, 2004, in forum: VHDL
    Replies:
    4
    Views:
    713
  4. ALuPin

    Back-Annotate Assignments

    ALuPin, Oct 20, 2004, in forum: VHDL
    Replies:
    1
    Views:
    1,809
    Ben Twijnstra
    Oct 20, 2004
  5. Philipp
    Replies:
    21
    Views:
    1,157
    Philipp
    Jan 20, 2009
Loading...

Share This Page