Raised This Month: $ Target: $400
 0% 

Working with string alteration and control...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
-Demonsthenes-
Junior Member
Join Date: Mar 2006
Old 03-20-2006 , 20:59   Working with string alteration and control...
Reply With Quote #1

I've been working with pawn on and off for about a year, but something I never quite understood was how to use the returned array from some sort of string function such as "clamp" or "max".
Code:
clamp(gaben,10,20)
That will return gaben with a value that is, or is in between 10 and 20. But it returns it (to my understanding), like this (let gaben = 10): gaben[0] = 1; gaben[1] = 0;. So, I have a peice of ugly code like this:

Code:
public function_setitcolor(id) {
	// This function was a bit awkward for me, I'm not sure of a better way to do it..
	read_argv(1,hs_glowcolor_r_r,3)
	read_argv(2,hs_glowcolor_g_r,3)
	read_argv(3,hs_glowcolor_b_r,3)
	if(read_argc() != 3 
	|| !is_str_num(hs_glowcolor_r_r)  
	|| !is_str_num(hs_glowcolor_g_r) 
	|| !is_str_num(hs_glowcolor_b_r)) { 
		console_print(id,"You need exactly three, numerical values for this command to work properly")
	}
	hs_glowcolor_r = hs_glowcolor_r_r[0]&hs_glowcolor_r_r[1]&hs_glowcolor_r_r[2]
	hs_glowcolor_g = hs_glowcolor_g_r[0]&hs_glowcolor_g_r[1]&hs_glowcolor_g_r[2]
	hs_glowcolor_b = hs_glowcolor_b_r[0]&hs_glowcolor_b_r[1]&hs_glowcolor_b_r[2]
}
How can I clean this up or (unfortunatly) is this the best way, or is this just wrong.
-Demonsthenes- is offline
xeroblood
BANNED
Join Date: Mar 2004
Location: Toronto, Canada
Old 03-20-2006 , 21:16  
Reply With Quote #2

Niether of those 2 functions return an array. I don't know what the value of 'gaben' is that you are passing to the function but:

max()
clamp()

They both return a single value, depending on the input. Clamp will make sure a number is within a specified range, and max just returns the higher of the 2 numbers you pass it.

Ex 1:

var1 = 501
var2 = clamp(var1, 0, 300)

Clamp will return: 300

Ex 2:

var1 = -45
var2 = clamp(var1, 0, 300)

Clamp will return: 0

Ex 3:

var1 = 501
var2 = 300
var3 = max(var1, var2)

Max will return: 501
xeroblood is offline
Send a message via MSN to xeroblood
-Demonsthenes-
Junior Member
Join Date: Mar 2006
Old 03-20-2006 , 21:19  
Reply With Quote #3

Then allow me to rephrase my question; What if the function DOES, as in read_argv().
-Demonsthenes- is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 03-20-2006 , 21:23  
Reply With Quote #4

You can either use a loop to clamp each cell in the array, or just clamp whichever you need.

I'm not exactly sure what your question is, but you never seem to have mentioned str_to_num. Perhaps you're looking for that?
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
xeroblood
BANNED
Join Date: Mar 2004
Location: Toronto, Canada
Old 03-20-2006 , 21:25  
Reply With Quote #5

You must be looking for str_to_num() then, but why are you getting 2 values for a single number? Like 10 broken into [0] = 1 and [1] = 0 ????
xeroblood is offline
Send a message via MSN to xeroblood
-Demonsthenes-
Junior Member
Join Date: Mar 2006
Old 03-20-2006 , 21:27  
Reply With Quote #6

My exact question looks something like this:


"When a function returns a array that is a string broken up into different pieces (I.E. read_argv(1,blah), will make blah = 102, blah[0] = 1, blah[1] = 0, blah[2] = 2), what is the best way to use this if the command you want to use won't accept an array".

I'm confusing, I know, sorry. That's the best way I can verbalize my question.

You HAVE to pass an array to read_argv, and the only way to use the information it gets (to my knowlage) is that way...
-Demonsthenes- is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 03-20-2006 , 21:29  
Reply With Quote #7

You're looking for str_to_num then.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
xeroblood
BANNED
Join Date: Mar 2004
Location: Toronto, Canada
Old 03-20-2006 , 21:34  
Reply With Quote #8

Wow, very confusing.. I don't know why you are getting your number as an array, but since you are, you could do some math on it:
Assuming:
blah[0] = 1
blah[1] = 0
blah[2] = 2

Code:
// let argc = the number of elements in the array

new max = power( 10, argc - 1 )
new myNum = 0

for( i = 0; i < argc; i++ )
{
    myNum += (blah[i] * max)
    max /= 10
}
then, 'myNum' variable should equal 102 as an integer..
After you get it as an integer, then you can use max or clamp on it:
new myNewNum = clamp(myNum, 1, 100)

myNewNum would equal 100

BUT:
If the value of your blah[] array has strings, like:
blah[0] = "1"
blah[1] = "0"
blah[2] = "2"

Then do this:
Code:
// let argc = the number of elements in the array

new max = power( 10, argc - 1 )
new myNum = 0

for( i = 0; i < argc; i++ )
{
    myNum += (str_to_num(blah[i]) * max)
    max /= 10
}
xeroblood is offline
Send a message via MSN to xeroblood
Reply



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 16:42.


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