Raised This Month: $ Target: $400
 0% 

[HELP] Switching two variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
AoD90
Senior Member
Join Date: Jul 2008
Location: Ugljevik/RS/BIH
Old 12-26-2008 , 14:53   [HELP] Switching two variables
Reply With Quote #1

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.
AoD90 is offline
Send a message via MSN to AoD90
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 12-26-2008 , 14:56   Re: [HELP] Switching two variables
Reply With Quote #2

Well...
PHP Code:
new 57;
new 
aux;

aux y;
aux;
x
__________________

anakin_cstrike is offline
AoD90
Senior Member
Join Date: Jul 2008
Location: Ugljevik/RS/BIH
Old 12-26-2008 , 14:59   Re: [HELP] Switching two variables
Reply With Quote #3

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
AoD90 is offline
Send a message via MSN to AoD90
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 12-26-2008 , 15:04   Re: [HELP] Switching two variables
Reply With Quote #4

I think this
PHP Code:
replacea)
{
    new 
aux;
    
    
aux a;
    
b;
    
aux;

__________________

anakin_cstrike is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 12-26-2008 , 18:30   Re: [HELP] Switching two variables
Reply With Quote #5

Quote:
Originally Posted by anakin_cstrike View Post
Well...
PHP Code:
new 57;
new 
aux;
 
aux y;
aux;
x
And that isn't even right.
[ --<-@ ] Black Rose is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 12-27-2008 , 05:39   Re: [HELP] Switching two variables
Reply With Quote #6

Yeah...i saw now.
__________________

anakin_cstrike is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-27-2008 , 12:04   Re: [HELP] Switching two variables
Reply With Quote #7

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?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 12-27-2008 , 12:49   Re: [HELP] Switching two variables
Reply With Quote #8

Quote:
Originally Posted by Exolent[jNr] View Post
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)
__________________

Community / No support through PM
danielkza is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 12-26-2008 , 15:25   Re: [HELP] Switching two variables
Reply With Quote #9

Anakin. That can't be done with global variables so you should return them.
SnoW is offline
Send a message via MSN to SnoW
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 12-26-2008 , 15:44   Re: [HELP] Switching two variables
Reply With Quote #10

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)
__________________

Community / No support through PM
danielkza is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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