Copying arrays
Ugha
Posts: 543
Whats the best way to copy one array into another?
Would this work:
put array1, array2
Or would it only copy the first element?
What if I wanted to exclude an element from the copy... is there an easier way to do that than just a long for-loop?
Also... if I have this situation:
Can I somehow transfer the address of the array into a placeholder or something with each if then do a single block of code on that placeholder?
This is an example of how·I first thought it'd work.
This of course doesn't work, but perhaps it'll give you a better understanding of what I'm asking.
Thanks in advance for your help.
PS: I'd run a bunch of tests on my own but I won't have access to my SX for at least a day and I want to work on code now.
Would this work:
put array1, array2
Or would it only copy the first element?
What if I wanted to exclude an element from the copy... is there an easier way to do that than just a long for-loop?
Also... if I have this situation:
array1 var byte(10) array2 var byte(10) array3 var byte(10) if num = 1 then ... long loop and other stuff that requires knowledge of which array it is... array1(0) = 99 elseif num = 2 then ... another long loop ... array2(0) = 99 elseif num = 3 then ... a third long loop... array3(0) = 99 endif
Can I somehow transfer the address of the array into a placeholder or something with each if then do a single block of code on that placeholder?
This is an example of how·I first thought it'd work.
array1 var byte(10) array2 var byte(10) array3 var byte(10) arrayX var byte if num = 1 then arrayX = @array1 elseif num = 2 then arrayX = @array2 elseif num = 3 then arrayX = @array3 endif ... Long loop .. arrayX(0) = 99
This of course doesn't work, but perhaps it'll give you a better understanding of what I'm asking.
Thanks in advance for your help.
PS: I'd run a bunch of tests on my own but I won't have access to my SX for at least a day and I want to work on code now.
Comments
I actually looked at the compiled output for this:
...and it seems to do what you want (good to know). Analyzing the generated code shows that PUT will move as many bytes as are in array2, so this should be equal to or less than the size of array1. For example, if you had these declarations:
The copy is fine but the first five elements of array2 become corrupted. In the attached graphic I've marked array1 in green and array2 in red -- note that the first five values of array2 have gone from 0, 1, 2, 3, 4 to 3, 4, 5, 6, 7 (test code is attached).
Finally, SX/B has an internal array called __RAM(), which is the entire RAM space, so you can do this:
Of course, you can modify pntr after the initial assignment to get to different elements of the array.
Post Edited (JonnyMac) : 2/16/2009 5:54:17 AM GMT
·Bean, you might want to look at this.· To make this work, I needed to use #$99 and not #99 as expected for my data type to get the compiler to give me 99 decimal. I'm using ver. 3.2.92h
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bad spellers of the world untie!
Post Edited (mojorizing) : 2/17/2009 4:54:45 AM GMT
Thanks,
kevin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bad spellers of the world untie!