Push to array reference

后端 未结 4 505
野的像风
野的像风 2020-12-29 02:40

Is it possible to push to an array reference in Perl? Googling has suggested I deference the array first, but this doesn\'t really work. It pushes to the defere

4条回答
  •  庸人自扰
    2020-12-29 03:25

    Yes its possible. This works for me.

    my @a = (); 
    my $aref = \@a; # creates a reference to the array a
    
    push(@$aref, "somevalue"); # dereference $aref and push somevalue in it
    
    print $a[0]; # print the just pushed value, again @$aref[0] should also work
    

    As has been mentioned, $aref = [@a] will copy and not create reference to a

提交回复
热议问题