创建数组数组,然后检索值

时间:2022-01-05 17:40:35

I have input the following code:

我输入了以下代码:

my @resultsArray;
my @dataArray;
while (my ($id, $originLat, $originLng, $compensation ) = $sth->fetchrow_array) {
    @dataArray = ($id, $originLat, $originLng, $compensation);
    print "ID: $id Lat: $originLat Lng: $originLng Compensation: $compensation\n";
    print "Data Array: @dataArray\n";
    #the above code words.
    #I declare 
    push (@resultsArray, @dataArray);
}

my (@r1, @r2, @r3, @r4);
#issue here
for  (@resultsArray) {
    @r1 = pop(@resultsArray);
    @r2 = pop(@resultsArray);
    @r3 = pop(@resultsArray);
    @r4 = pop(@resultsArray);               

    print "ID: $r4[0] Lat: $r3[0] Lng: $r2[0] Compensation: $r1[0]\n";
    #@r1 = ();
}

The above code works. The array @dataArray is pushed onto the @resultsArray stack and is popped into the @r1, @r2, @r3, @r4 respectively. However this is terrible code. Is there a better way, a cleaner way with less lines that is still readable and that is generally more professional?

上面的代码有效。数组@dataArray被推送到@resultsArray堆栈并分别弹出到@ r1,@ r2,@ r3,@ r4。然而,这是可怕的代码。有没有更好的方法,更简洁的方式,更少的线仍然可读,而且通常更专业?

I'm looking to improve my skills. I feel like I might be the laughing stock of a room if I show up with something like this.

我正在寻求提高自己的技能。如果我出现这样的话,我觉得我可能是房间的笑柄。

1 个解决方案

#1


2  

You just need to learn how to use references. Here's your code rewritten with array references:

您只需要学习如何使用引用。这是用数组引用重写的代码:

my @resultsArray;
while (my ($id, $originLat, $originLng, $compensation ) = $sth->fetchrow_array) {
    my $dataArray = [$id, $originLat, $originLng, $compensation];
    print "ID: $id Lat: $originLat Lng: $originLng Compensation: $compensation\n";
    print "Data Array: @$dataArray\n";
    #the above code words.
    #I declare 
    push @resultsArray, $dataArray;
}

for my $r (@resultsArray) {
    print "ID: $r->[0] Lat: $r->[1] Lng: $r->[2] Compensation: $r->[3]\n";
}

Arrays of arrays in Perl are best represented as arrays of array references.

Perl中的数组数组最好表示为数组引用的数组。

The magic here is

这里的魔力是

my $dataArray = [$id, $originLat, $originLng, $compensation];

This creates an anonymous array containing the four values, and then sets the scalar $dataArray to point to the anonymous array. This reference then gets pushed onto @resultsArray. @resultsArray is now an array of scalars that happen to be references to other arrays. Those references can be de-referenced using the ->[n] construct to get individual members of the array.

这将创建一个包含四个值的匿名数组,然后将标量$ dataArray设置为指向匿名数组。然后将此引用推送到@resultsArray。 @resultsArray现在是一个标量数组,恰好是对其他数组的引用。可以使用 - > [n]构造取消引用这些引用,以获取数组的各个成员。

#1


2  

You just need to learn how to use references. Here's your code rewritten with array references:

您只需要学习如何使用引用。这是用数组引用重写的代码:

my @resultsArray;
while (my ($id, $originLat, $originLng, $compensation ) = $sth->fetchrow_array) {
    my $dataArray = [$id, $originLat, $originLng, $compensation];
    print "ID: $id Lat: $originLat Lng: $originLng Compensation: $compensation\n";
    print "Data Array: @$dataArray\n";
    #the above code words.
    #I declare 
    push @resultsArray, $dataArray;
}

for my $r (@resultsArray) {
    print "ID: $r->[0] Lat: $r->[1] Lng: $r->[2] Compensation: $r->[3]\n";
}

Arrays of arrays in Perl are best represented as arrays of array references.

Perl中的数组数组最好表示为数组引用的数组。

The magic here is

这里的魔力是

my $dataArray = [$id, $originLat, $originLng, $compensation];

This creates an anonymous array containing the four values, and then sets the scalar $dataArray to point to the anonymous array. This reference then gets pushed onto @resultsArray. @resultsArray is now an array of scalars that happen to be references to other arrays. Those references can be de-referenced using the ->[n] construct to get individual members of the array.

这将创建一个包含四个值的匿名数组,然后将标量$ dataArray设置为指向匿名数组。然后将此引用推送到@resultsArray。 @resultsArray现在是一个标量数组,恰好是对其他数组的引用。可以使用 - > [n]构造取消引用这些引用,以获取数组的各个成员。