AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Working with string alteration and control... (https://forums.alliedmods.net/showthread.php?t=25817)

-Demonsthenes- 03-20-2006 20:59

Working with string alteration and control...
 
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.

xeroblood 03-20-2006 21:16

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

-Demonsthenes- 03-20-2006 21:19

Then allow me to rephrase my question; What if the function DOES, as in read_argv().

Hawk552 03-20-2006 21:23

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?

xeroblood 03-20-2006 21:25

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 ????

-Demonsthenes- 03-20-2006 21:27

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...

Hawk552 03-20-2006 21:29

You're looking for str_to_num then.

xeroblood 03-20-2006 21:34

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
}



All times are GMT -4. The time now is 16:42.

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