Assign address of one array to another in Perl possible?

后端 未结 4 1503
深忆病人
深忆病人 2021-01-29 02:07

In the following C# code:

int[] X = new int[2];
X[0] = 1;
X[1] = 2;
int[] Y = X;
X[1] = 3;

After this executes, Y[1] will also be 3 since the o

4条回答
  •  梦如初夏
    2021-01-29 02:54

    Try doing this :

    my @X = (1, 2);
    my $ref = \@X;        # $ref in now a reference to @X array (the magic is `\`)
    $ref->[0] = "foobar"; # assigning "foobar" to the first key of the array
    print join "\n", @X;  # we print the whole @X array and we see that it was changed
    

提交回复
热议问题