0

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?

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.

2
  • 1
    Why do you use arrays for each of @r1, @r2, @r3, @r4? You seem to be using them as scalars. splice() is your friend. To take last 4 elements off the array, you can use: @r = splice @resultsArray, -4. Then you can utilize $r[0] .. $r[3]. Commented Feb 7, 2013 at 2:55
  • That's really slick Steven. Thanks. Shaved a lot off my code and made it look better. Thanks again. Commented Feb 7, 2013 at 3:00

1 Answer 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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.