Perl array vs list

前端 未结 6 1203
余生分开走
余生分开走 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:36

    If you want more clarification, see the relevant documentation.

    You could try and do some things for illustration purposes:

    #!perl -lw # -l appends a \n to each print, -w enables warnings
    use strict;
    my $aryref = [1 .. 5];
    print for $aryref;    # Prints the stringified reference - not what you want
    print for @$aryref;   # Dereferencing it lets you access the array
    my @ary = $aryref;    # Probably not what you want
    print for @ary;       # Stringified reference again
    print for @{$ary[0]}; # Dereference the first array element (which holds the ref)
    

提交回复
热议问题