AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP] Switching two variables (https://forums.alliedmods.net/showthread.php?t=82608)

AoD90 12-26-2008 14:53

[HELP] Switching two variables
 
I need a function for switching two variables.
In Pascal it would look like this:
Code:

procedure smt(var x,y:real);
var p:real;
p:=x;
x:=y;
y:=p;
end;

I need this in pawn.

anakin_cstrike 12-26-2008 14:56

Re: [HELP] Switching two variables
 
Well...
PHP Code:

new 57;
new 
aux;

aux y;
aux;
x


AoD90 12-26-2008 14:59

Re: [HELP] Switching two variables
 
Is there a way to put this in some function so I could call it like this:

replace(var1,var2)

and it will replace those two vars

anakin_cstrike 12-26-2008 15:04

Re: [HELP] Switching two variables
 
I think this
PHP Code:

replacea)
{
    new 
aux;
    
    
aux a;
    
b;
    
aux;



SnoW 12-26-2008 15:25

Re: [HELP] Switching two variables
 
Anakin. That can't be done with global variables so you should return them.

danielkza 12-26-2008 15:44

Re: [HELP] Switching two variables
 
Code:

new a = 2, b = 4
server_print("a=%d, b=%d", a, b)

a ^= b
b ^= a
a ^= b

server_print("a=%d, b=%d", a, b)


[ --<-@ ] Black Rose 12-26-2008 18:30

Re: [HELP] Switching two variables
 
Quote:

Originally Posted by anakin_cstrike (Post 733600)
Well...
PHP Code:

new 57;
new 
aux;
 
aux y;
aux;
x


And that isn't even right.

anakin_cstrike 12-27-2008 05:39

Re: [HELP] Switching two variables
 
Yeah...i saw now.

Exolent[jNr] 12-27-2008 12:04

Re: [HELP] Switching two variables
 
SnoW, yes it can:
Code:
stock replaceint(&a, &b) {     new temp = a;     a = b;     b = temp; }

daniel, won't that only work because those are 2 numbers that are able to do that?
What about 3 and 7?

danielkza 12-27-2008 12:49

Re: [HELP] Switching two variables
 
Quote:

Originally Posted by Exolent[jNr] (Post 734050)
SnoW, yes it can: Code:
stock replaceint(&a, &b) { new temp = a; a = b; b = temp; }


daniel, won't that only work because those are 2 numbers that are able to do that?
What about 3 and 7?

It works for everything
Code:

new a = 3, b = 7 //a = 011b, b = 111b)

a ^= b // 011b ^ 111b = 100b
b ^= a // 111b ^ 100b = 011b
a ^= b // 100b ^ 011b = 111b

// a == 7, b == 3 //a = 111b, b = 011b)



All times are GMT -4. The time now is 23:10.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.