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.
@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].