Determining variables?
#1

Is there anyway to add a function that determines if the given variable is a float/string/int?
Reply
#2

There is one for PlayerVariables: GetPVarType(playerid, varname[]);
I couldn't find any function for non-player related variables. But I do think it's possible to create such function...
Reply
#3

Quote:
Originally Posted by Schneider
Посмотреть сообщение
There is one for PlayerVariables: GetPVarType(playerid, varname[]);
I couldn't find any function for non-player related variables. But I do think it's possible to create such function...
I am about to make a database system with MySQL hence I need to make functions that will store data easier than actually typing the int / string / float.

Off Topic are you sam schneider?
Reply
#4

I've been playing around a bit, and came up with a function that checks if a string has a integer, float or string format, but you still have to convert the actual variable to a string first, but before you can do that you have to know if the variable is an integer, float or string, making this whole function useless for your needs.

I've searched ****** and this forum for a long time, but couldn't find a solution how to pass a variable, ignoring the fact if it's a integer, string or floating point number.
I hope somebody else here knows a solution for this problem!

I'm not sure if it's 100% foolproof though..

It will return 0 if the entered string is an integer i.e.: "43941", "-3139".
It will return 1 if the entered string is a float: i.e.: "3109.310", "-1.31".
It will return 2 if the entered string contains any other characters: ".3123", "ajd913", "-39Fdn2".

pawn Код:
stock GetVarType(variable[])
{
    new length = strlen(variable), dots, letters, bool:negative = false;
    if(variable[0] == '-') negative = true;
    for(new i; i<length; i++)
    {
        if(negative == false) {
            if((variable[i] == '.') && (i>0) && i<(length-1)) dots++;
        else if((variable[i] > '9') || (variable[i] < '0')) letters++;
        }
        else {
            if((variable[i] == '.') && (i>1) && i<(length-1)) dots++;
            else if(((variable[i] > '9') || (variable[i] < '0')) && (i != 0)) letters++;
        }
    }
    if(dots == 1 && letters == 0) return 1;  // float
    else if(letters > 0 ) return 2; //string
    return 0; // integer
}
Offtopic: No I'm not Sam.
Reply
#5

Quote:
Originally Posted by Schneider
Посмотреть сообщение
I've been playing around a bit, and came up with a function that checks if a string has a integer, float or string format, but you still have to convert the actual variable to a string first, but before you can do that you have to know if the variable is an integer, float or string, making this whole function useless for your needs.

I've searched ****** and this forum for a long time, but couldn't find a solution how to pass a variable, ignoring the fact if it's a integer, string or floating point number.
I hope somebody else here knows a solution for this problem!

I'm not sure if it's 100% foolproof though..

It will return 0 if the entered string is an integer i.e.: "43941", "-3139".
It will return 1 if the entered string is a float: i.e.: "3109.310", "-1.31".
It will return 2 if the entered string contains any other characters: ".3123", "ajd913", "-39Fdn2".

pawn Код:
stock GetVarType(variable[])
{
    new length = strlen(variable), dots, letters, bool:negative = false;
    if(variable[0] == '-') negative = true;
    for(new i; i<length; i++)
    {
        if(negative == false) {
            if((variable[i] == '.') && (i>0) && i<(length-1)) dots++;
        else if((variable[i] > '9') || (variable[i] < '0')) letters++;
        }
        else {
            if((variable[i] == '.') && (i>1) && i<(length-1)) dots++;
            else if(((variable[i] > '9') || (variable[i] < '0')) && (i != 0)) letters++;
        }
    }
    if(dots == 1 && letters == 0) return 1;  // float
    else if(letters > 0 ) return 2; //string
    return 0; // integer
}
Offtopic: No I'm not Sam.
I havent got to read over the script since I'm focused on a house system I'm scripting... in short, if I convert an int / float to a string and use this function with it, will it tell me if the integer is an integer?

E.G:

Input: "5" output: 0
Input: "6745.7943" output: 1
Input: "3 cookies!" output: 2
Reply
#6

Yes, but the problem is before you convert a integer or float to a string, you have to know if it's either a integer or float beforehand... so that makes this function useless.

At this moment you can only pass strings, if you enter a variable that is a integer or float it will give you a Tag-Mismatch error:

pawn Код:
new Integer = 540;
new Float:FloatingPoint = 3.14;
new StringString[12] = "Hello";
new StringFloat[12] = "30.1391";

new Type0 = GetVarType(Integer); // this will result in a tag-mismatch error
new Type1 = GetVarType(FloatingPoint);  // this will result in a tag-mismatch error
new Type2 = GetVarType(StringString); // This will work and return '2'.
new Type3 = GetVarType(StringFloat); // This will work too and returns '1'.
Too bad there is no function (as far as I know) that converts anything (regardless the variable-type) into a string.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)