peterchan75
Supremacy Member
- Joined
- Apr 26, 2003
- Messages
- 6,719
- Reaction score
- 529
Hi All,
I have a 2 dimensional array and the second column has mm/dd/yyyy format date. The code below change the date to yyyymmdd format and sort the array by date in ascending order and store the result to array2 and convert the date from yyyymmdd back to mm/dd/yyyy in array2.
I do a print the first row and second column of array1 and I get back the old date format of mm/dd/yyyy.
Why does column 2 of array1 revert back to the old data ?
I have a 2 dimensional array and the second column has mm/dd/yyyy format date. The code below change the date to yyyymmdd format and sort the array by date in ascending order and store the result to array2 and convert the date from yyyymmdd back to mm/dd/yyyy in array2.
Code:
for (my $i=0;$i<=$#array1;$i++) { #Convert date from MM/DD/YYYY to YYYYMMDD
my @mmddyyyy = split('/',$array1[$i][1]);
$array1[$i][1] = int($mmddyyyy[2])*10000+int($mmddyyyy[0])*100+int($mmddyyyy[1])*1;
}
my @array2 = sort { $a->[1] <=> $b->[1] } @array1;
for (my $i=0;$i<=$#array2;$i++) { #Convert date from YYYYMMDD to MM/DD/YYYY
$array2[$i][1] = substr($array2[$i][1],4,2) . '/' . substr($array2[$i][1],6,2) . '/' . substr($array2[$i][1],0,4);
}
I do a print the first row and second column of array1 and I get back the old date format of mm/dd/yyyy.
Why does column 2 of array1 revert back to the old data ?
Then the "reference" word pop up! @Array2 is referenced to @Array1. So, whatever I have done to @Array2 will affect @Array1.
I have to create a dereference array first.