|
Post by enerfree on Jan 8, 2019 19:25:43 GMT -5
Hello, I'm looking for the equivalent of the Omikron's statement "Swap". For example : A=1: B=2: Swap A,B: print A;B 2 1 How do I do that ?
it would be nice if you could help me.
|
|
|
Post by Brandon Parker on Jan 8, 2019 19:48:31 GMT -5
There is no direct swap command available that I am aware of, but you can easily roll your own.
var1 = 1 var2 = 2
Call Swap var1, var2
var1 = 1 var2 = 2
Print var1 Print var2 Print 'Print a blank space
Call Swap var1, var2
Print var1 Print var2 Wait
Sub Swap ByRef varA, ByRef varB Swap = varA varA = varB varB = Swap End Sub
{:0)
Brandon R. Parker
|
|
|
Post by Brandon Parker on Jan 8, 2019 19:53:10 GMT -5
And.....if you wanted to do it without the temporary variable then you could do it like the code below, but it takes more operations for the CPU to perform and doesn't really gain anything except the instantiation/ destruction of the temporary variable.
Sub Swap2 ByRef varA, ByRef varB varA = (varA + varB) varB = (varA - varB) varA = (varA - varB) End Sub
{:0)
Brandon R. Parker
|
|
|
Post by enerfree on Jan 9, 2019 17:05:34 GMT -5
Oh, yeah, I see. Thank you very much. I'll try that.
|
|
|
Post by tsh73 on Jan 10, 2019 6:48:24 GMT -5
Keep in mind that all arrays in LB are global and byRef does not work on array elements. So
call Swap a(i), a(j)
will not work.
|
|
|
Post by enerfree on Feb 1, 2019 13:43:51 GMT -5
I don't think about it night and day but actually it does not work. Sorry for the purists, would you see the following as exemple:
1 dim a(11) 2 x=11 3 for b=1 to 11 4 a(b)=b*2 5 next b 6 w=int(x/2) 7 for b=1 to w 8 y=a(x) 9 z=a(b) 10 a(b)=y 11 a(x)=z 12 x=x-1 13 next b 14 for d=1 to 11 15 print a(d) 16 next d
It works fine. Do you think smaller is possible ?
|
|
|
Post by Brandon Parker on Feb 3, 2019 17:12:05 GMT -5
I'm not really understanding what you are saying....
First you say that it doesn't work when the code you posted appears to work for me. Then you go on to say that it works and ask if smaller is possible. Please restate your question/ concern....
Also, please use code tags around your code and format for readability. Dropping the line numbers might also be welcome as they are a thing of the past.
{:0)
Brandon Parker
|
|