You also have an off-by-one error. $length contains the number of elements in the array, which is one beyond the end of the array.
The $i+=2 in your loop does nothing. The for will put the next value from the list into $i without checking its current value. You want
for ($i=0; $i<$length2; $i+=2){
push @array2, $array[$i];
}
for ($i=1; $i<$length; $i+=2){
push @array2, $array[$i];
}
or
for $i(0..($length-1)/2){
push @array2, $array[$i*2];
}
for $i(0..($length-1)/2){
push @array2, $array[$i*2+1];
}
or
for $i(0..$#array/2){
push @array2, $array[$i*2];
}
for $i(0..$#array/2){
push @array2, $array[$i*2+1];
}
That doesn't fix your decrpytion (which is totally wrong), but it puts you at a point where you can start having a look at it. |