I figured out this 'feature' of the S.E. last night after swearing at my computer for three hours. I'd like to save all of you who are thinking of venturing into the wonderful world of matrices the heartache.
You've probably already figured out that you can append one array to another to create a matrix:
Code: Select all
001 $matrix = array alloc: size = 0
002 $row1 = array alloc: size = 2
003 $row2 = array alloc: size = 2
004 row1[0] = 1
005 row1[1] = 2
006 row2[0] = 3
007 row2[1] = 4
008 append $row1 to $matrix
009 append $row2 to $matrix
Code: Select all
$matrix=
1 2
3 4
This can become an issue.
If you are using a loop to 'build' an array:
Code: Select all
001 $return = array alloc: size=0
002 $temprow = array alloc: size=2
003
004 $i = 0
005 while $i < 2
006 if $i == 0
007 $temprow[0] = 1
008 $temprow[1] = 2
009 else
010 $temprow[0] = 3
011 $temprow[1] = 4
012 end
013 inc $i =
014 append $temprow to array $return
015 end
Code: Select all
$return=
1 2
3 4
Code: Select all
$return=
3 4
3 4
You append $temprow to $return. It stores a pointer to the array $temprow, it does not store 1 2. When you later edit $temprow the next time through the loop, it changes the first row in matrix $return.
Then, it of course appends the changed row as the second entry in the matix.
To get around this you must reallocate / reinitialize the temporary array every time after you use it. The following code will work correctly:
Code: Select all
001 $return = array alloc: size=0
002 $temprow = array alloc: size=2
003
004 $i = 0
005 while $i < 2
006 if $i == 0
007 $temprow[0] = 1
008 $temprow[1] = 2
009 else
010 $temprow[0] = 3
011 $temprow[1] = 4
012 end
013 inc $i =
014 append $temprow to array $return
015 $temprow = array alloc: size=2
016 end
I'll post more as I figure more out.