How to extract number from a string?

Posted By: tolu619

How to extract number from a string? - 11/17/14 16:46

I'm trying to check if the player is in any region and if so, check if the region name starts with the word "abyss". If it does, it would be "abyss1", "abyss2", "abyss3", etc. So I want to extract the number at the end of the string and convert it to an integer. I've played around with most of the string manipulation functions defined in strio.c but somehow, I keep getting empty pointer exceptions.
Code:
//GLOBAL VARAIBLES
STRING* AbyssNameString = str_create("");
STRING* AbyssNumberString = str_create("");
STRING* TempString = str_create("");
STRING* ComparisonString = str_create("Abyss");

//THESE LINES ARE INSIDE PLAYER'S WHILE(1) LOOP
if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
            if(str_cmpni(AbyssNameString, "Abyss")) //if the region is an abyss
            {        	
        	    str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'        
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }
//THIS DOESN'T WORK


Code:
if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
TempString = str_cut(NULL, AbyssNameString, 1, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, "Abyss")) //if the region is an abyss
            {        	
        	    str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'        
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }
//THIS DOESN'T WORK


Code:
if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
TempString = str_cut(NULL, AbyssNameString, 1, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, ComparisonString)) //if the region is an abyss
            {        	
        	    str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'        
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }
//THIS DOESN'T WORK


And I tried many other similar permutations
Posted By: Kartoffel

Re: How to extract number from a string? - 11/17/14 17:48

why don't you just use clip/trunc (don't know which of these two it was, always mix them up) to cut off the fist couple of letters and the use str_to_int?

oh and that's not how you define global vars:
Code:
STRING* AbyssNameString = str_create("");
STRING* AbyssNumberString = str_create("");
STRING* TempString = str_create("");
STRING* ComparisonString = str_create("Abyss");

Posted By: tolu619

Re: How to extract number from a string? - 11/18/14 14:18

I thought the global vars might be the problem but it didn't make a difference when I declared them as
Code:
STRING* AbyssNameString = "";
STRING* AbyssNumberString = "";
STRING* TempString = "";
STRING* ComparisonString = "";


Just to be sure, I just declared them this way again and used str_clip and it still didn't work
Code:
if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
        	wait(5);
        	str_printf(AbyssNameString); //IT CRASHES BEFORE I EVEN GET THE RESULTS OF THIS LINE
        	str_cpy(TempString, AbyssNameString);
        	
            
            str_clip(TempString, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, "Abyss")) //if the region is an abyss
            {        	
        	    str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'        
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }

Posted By: Kartoffel

Re: How to extract number from a string? - 11/18/14 14:23

you kind of misunderstood this...
you use str_cmp(ni) to check for 'abyss'. after that you delete the first 5 letters of the string, which means the number will be at the beginning of the string and str_to_int will do it's job.
Posted By: tolu619

Re: How to extract number from a string? - 11/18/14 20:51

Doesn't this delete the first 5 characters and store only the numerical part?
Code:
str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'



What bothers me is that I think it's crashing right from the first if statement. My str_printf line doesn't print anything when I run my tests.
Posted By: 3run

Re: How to extract number from a string? - 11/18/14 22:32

First of all, you've created this topic in wrong forum (mods please move it to the correct one?), "User Resources" - forum used to share something with community, it's used for contributions!

About your question, take a look at this example:
Code:
// get the name of the region via 'region_find'!
// then save it into temporary string:
STRING* regName = "Abyss1";
STRING* tempStr = "#10";
str_cpy(tempStr, regName);
// then, you have to check if we've entered any of "Abyss" regions:
if(str_cmp(str_trunc(tempStr, 1), "Abyss")){
           // after that, you can check for the number of the region:
           if(str_cmp(str_parse(NULL, regName, 6), "1")){
                       // entered first Abyss region here!
           }
}

This example works, as I've tested it right before sharing! Probably not the best way, but should handle your situation without any troubles!

Greets
Posted By: tolu619

Re: How to extract number from a string? - 11/18/14 22:43

Thanks! I'll try it out! The topic creation thing must have been on User Resources by default, I didn't edit it at all. I didn't even notice the option to do so.
Posted By: 3run

Re: How to extract number from a string? - 11/19/14 06:03

Originally Posted By: tolu619
The topic creation thing must have been on User Resources by default, I didn't edit it at all. I didn't even notice the option to do so.
You can create topics under almost any forum here, next time ask for help under "Lite-C Programming".
Posted By: tolu619

Re: How to extract number from a string? - 11/20/14 10:42

Originally Posted By: 3run
Originally Posted By: tolu619
The topic creation thing must have been on User Resources by default, I didn't edit it at all. I didn't even notice the option to do so.
You can create topics under almost any forum here, next time ask for help under "Lite-C Programming".

Yes sir! Can't I change which forum my topic falls under myself after creation?
Posted By: tolu619

Re: How to extract number from a string? - 11/20/14 10:49

Thanks, 3run!
I was finally able to solve it. For some reason, global variables didn't work. So I declared the variables inside my player's function and extracted the numbers like so:
Code:
function PlayerMovementFunction(MONSTER* Mon)
{
	STRING* AbyssNameString = str_create("");
    STRING* AbyssNumberString = str_create("");
    STRING* TempString =  str_create("#10");
while(1)
	{
		vec_set(vMin,my.x);
        vec_set(vMax,my.x);
        vec_add(vMin,my.min_x);
        vec_add(vMax,my.max_x);
        
        //CHECK IF PLAYER HAS FALLEN INTO AN ABYSS REGION, THEN RESPAWN HIM AT CORRESPONDING RESPAWN POINT
        if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
        	str_cpy(TempString, AbyssNameString);
        	TempString = str_cut(NULL, AbyssNameString, 1, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, "Abyss")) //if the region is an abyss
            {
        	    str_cpy(AbyssNumberString, AbyssNameString);
        	    str_clip(AbyssNumberString, 5); //Delete first 5 characters then store resulting number
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }


I decided to name any Abyss region that isn't a double digit as Abyss01, Abyss02, etc. It still works that way, and I doubt I'LL ever have up to 99 Abysses in a level, so I can safely hard code lines like "str_clip(AbyssNumberString, 5);" knowing that my Abyss region names are always 5 letters followed by 2 digits.
Posted By: 3run

Re: How to extract number from a string? - 11/20/14 12:30

Originally Posted By: tolu619
Originally Posted By: 3run
Originally Posted By: tolu619
The topic creation thing must have been on User Resources by default, I didn't edit it at all. I didn't even notice the option to do so.
You can create topics under almost any forum here, next time ask for help under "Lite-C Programming".

Yes sir! Can't I change which forum my topic falls under myself after creation?
I'm not sure if you can do this.

Originally Posted By: tolu619
Thanks, 3run!
I was finally able to solve it. For some reason, global variables didn't work. So I declared the variables inside my player's function and extracted the numbers like so:
Code:
function PlayerMovementFunction(MONSTER* Mon)
{
	STRING* AbyssNameString = str_create("");
    STRING* AbyssNumberString = str_create("");
    STRING* TempString =  str_create("#10");
while(1)
	{
		vec_set(vMin,my.x);
        vec_set(vMax,my.x);
        vec_add(vMin,my.min_x);
        vec_add(vMax,my.max_x);
        
        //CHECK IF PLAYER HAS FALLEN INTO AN ABYSS REGION, THEN RESPAWN HIM AT CORRESPONDING RESPAWN POINT
        if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
        	str_cpy(TempString, AbyssNameString);
        	TempString = str_cut(NULL, AbyssNameString, 1, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, "Abyss")) //if the region is an abyss
            {
        	    str_cpy(AbyssNumberString, AbyssNameString);
        	    str_clip(AbyssNumberString, 5); //Delete first 5 characters then store resulting number
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }


I decided to name any Abyss region that isn't a double digit as Abyss01, Abyss02, etc. It still works that way, and I doubt I'LL ever have up to 99 Abysses in a level, so I can safely hard code lines like "str_clip(AbyssNumberString, 5);" knowing that my Abyss region names are always 5 letters followed by 2 digits.
I'm happy that you've got it to work. But be aware that 'str_clip' will change 'AbyssNumberString', so you'll have to copy saved region name into it each time before 'clipping' it. You can easily change the code, so it will check for 999 regions as well, but only if you need this.

Greets
© 2024 lite-C Forums