Assign address of one array to another in Perl possible?

后端 未结 4 1458
深忆病人
深忆病人 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:40

    In Perl an array is not a pointer.

    You can get the reference of an array with the \ operator:

    my @array = ( 1, 2 );
    my $array_ref = \@array;
    

    $array_ref will then point to the original array (as in C)

    ${$array_ref}[0] = 3
    

    will change the first cell of the original array (i.e., $array[0] will be 3)

提交回复
热议问题