How do you swap Arrays?
BlitzMax Forums/BlitzMax Beginners Area/How do you swap Arrays?
| ||
Hi, I am trying to write a function to swap the contents of 2 arrays and for the life of me I can't work what I'm doing wrong. I've tried google but as yet not found anything helpful. Here is my code: Local a:Int[6] Local b:Int[6] SeedRnd MilliSecs() ' fill array with random stuff For t=0 To 5 a[t]=Rnd(1,100) b[t]=Rnd(1,100) Next 'show initial array show_list(a,b) ' swap arrays swap(a,b) ' show it again show_list(a,b) End Function show_list:Int(l1:Int[],l2:Int[]) For t=0 To 5 Print t + "] " + l1[t] + " | " + l2[t] Next Print "------------------" End Function Function swap(array1:Int[],array2:Int[]) Local tmp:Int[6] tmp = array1 array1 = array2 array2 = tmp End Function Using the debugger, I see that the arrays swap in the "swap" function, however it doesn't seem to affect the original arrays as they don't swap. Any suggestions would be greatly appreciated thanks |
| ||
Hello, the parameters in a function are actually copies and operate only in the scope of the function. If you want to manipulate the original arrays just add "Var" after the parameter like; Function swap(array1:int[] Var, array2:int[] Var) -Henri |
| ||
Thank you Henri that worked and is exactly what I was looking for. Thanks again |