How do I access arrays of array in perl?

流过昼夜 提交于 2019-12-08 14:07:10

问题


Hi I have a array as myarray. I would like to make a list as '1 2 3' which is joining the first subarray. My string is printing the memory location I suppose instead of list. any help will be appreciated.

@myarray = [[1,2,3],[4,5,6],[7,8,9]];
for (my $i=0; $i < @myarray; $i++) {
my @firstarray = $myarray[$i];
my $mystring = join("", @firstarray);
print "My string ".$mystring . ". "\n";
}

回答1:


You have to dereference the inner array reference by @{ ... }. Also, do not use [...] for the top structure - use normal parentheses (square brackets create an array reference, not an array). There was also a problem with the concatenation on your print line:

@myarray = ( [1,2,3], [4,5,6], [7,8,9] );
for (my $i=0; $i < @myarray; $i++) {
    my @firstarray = @{ $myarray[$i] };
    my $mystring = join("", @firstarray);
    print "My string " . $mystring . ".\n";
}



回答2:


You should use the Data::Dumper module, that way, that will help you to know how to parse your data structure :

print Dumper \@myarray; # force passing array as ref
$VAR1 = [
          [
            [
              1,
              2,
              3
            ],
            [
              4,
              5,
              6
            ],
            [
              7,
              8,
              9
            ]
          ]
        ];

But using the @ sigil (array) to store an ARRAY ref is strange, a $ sigil (scalar) is used most of the times for that purpose. (a reference is like a C pointer : an address to a memory cell. So its' a simple string, no need something else than a scalar to store it)

Then, you need to de-reference with the -> operator.

Ex :

$ perlconsole
Perl Console 0.4

Perl> my $arrayref = [[1,2,3],[4,5,6],[7,8,9]];

Perl> print join "\n", @{ $arrayref->[2] }
7
8
9



回答3:


You actually have an array of array of array.

  • The outer array has one element, a reference to an array.
    $myarray[0]
  • That referenced array has three elements, each a reference to an array.
    $myarray[0][0..2]
  • Each of those referenced arrays have three elements, three numbers.
    $myarray[0][0..2][0..2]

You want

my @aoa = ([1,2,3],[4,5,6],[7,8,9]);
   ^       ^       ^       ^
   |        \------+------/
   |            3 inner
1 outer

$aoa[$i][$j]

for my $inner (@aoa) {
   print(join(', ', @$inner), "\n");
}

or

my $aoa = [[1,2,3],[4,5,6],[7,8,9]];
          ^^       ^       ^
          | \------+------/
          |      3 inner
       1 outer

$aoa->[$i][$j]

for my $inner (@$aoa) {
   print(join(', ', @$inner), "\n");
}



回答4:


You need to change how you initialize your array so that () is used for the outer array bounds and [] for the inner arrays, which means that they are declared as references that will later need to be cast into their native array format for processing (my @subarray = @{$myarray[$i]};)

my @myarray = ([1,2,3], [4,5,6], [7,8,9]);

for (my $i=0; $i < @myarray; $i++) 
{
    my @subarray = @{$myarray[$i]};
    my $subarrayStr = join("", @subarray);
    print $i.". Subarray Str = ".$subarrayStr."\n";
}



回答5:


$myarray = [[1,2,3],[4,5,6],[7,8,9]];
printf "My string %s\n", join(" ", @{$myarray->[0]});

[[1,2,3],[4,5,6],[7,8,9]] returns a reference to the list of lists, not a list.

Change the @ into $ , to make $myarray a variable.

@{$myarray->[0]} will dereference the first sublist and return you the list you can use.

To print all three lists:

$myarray = [[1,2,3],[4,5,6],[7,8,9]];
map{printf "My string %s\n", join(" ",@{$_})} @{$myarray};


来源:https://stackoverflow.com/questions/15486604/how-do-i-access-arrays-of-array-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!