Perl array vs list

前端 未结 6 1221
余生分开走
余生分开走 2020-11-28 05:01

I have two data structures in Perl:

An array:

my @array2 = ( \"1\", \"2\", \"3\");

for $elem (@array2) {
    print $elem.\"\\n\";
}
<
6条回答
  •  时光说笑
    2020-11-28 05:49

    @array = ("1","2","3"); Here 1,2,3 is the element of @array variable. For ex. $array[0] is 1 , $array[1] is 2 and $array[2] is 3.

    @array = ["1","2","3"]; Perl uses anonymous array references using [ ], so here basically only one element that you are storing, and that is the reference of an array ["1","2","3"] to the @array variable. for ex $array[0] is "ARRAY(0x9c90818)"
    Hence while printing it is showing you the references.

提交回复
热议问题