1 registered members (AndrewAMD),
691
guests, and 7
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
How to extract number from a string?
#447112
11/17/14 16:46
11/17/14 16:46
|
Joined: Jun 2014
Posts: 97 Lagos, Nigeria
tolu619
OP
Junior Member
|
OP
Junior Member
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
|
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.
//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
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
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
|
|
|
Re: How to extract number from a string?
[Re: tolu619]
#447115
11/17/14 17:48
11/17/14 17:48
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
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:
STRING* AbyssNameString = str_create("");
STRING* AbyssNumberString = str_create("");
STRING* TempString = str_create("");
STRING* ComparisonString = str_create("Abyss");
Last edited by Kartoffel; 11/17/14 17:56.
POTATO-MAN saves the day! - Random
|
|
|
Re: How to extract number from a string?
[Re: Kartoffel]
#447127
11/18/14 14:18
11/18/14 14:18
|
Joined: Jun 2014
Posts: 97 Lagos, Nigeria
tolu619
OP
Junior Member
|
OP
Junior Member
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
|
I thought the global vars might be the problem but it didn't make a difference when I declared them as
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
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)]);
}
}
}
|
|
|
Re: How to extract number from a string?
[Re: tolu619]
#447128
11/18/14 14:23
11/18/14 14:23
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
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.
POTATO-MAN saves the day! - Random
|
|
|
Re: How to extract number from a string?
[Re: Kartoffel]
#447129
11/18/14 20:51
11/18/14 20:51
|
Joined: Jun 2014
Posts: 97 Lagos, Nigeria
tolu619
OP
Junior Member
|
OP
Junior Member
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
|
Doesn't this delete the first 5 characters and store only the numerical part?
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.
|
|
|
Re: How to extract number from a string?
[Re: tolu619]
#447130
11/18/14 22:32
11/18/14 22:32
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
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:
// 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
|
|
|
Re: How to extract number from a string?
[Re: tolu619]
#447132
11/19/14 06:03
11/19/14 06:03
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
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".
|
|
|
Re: How to extract number from a string?
[Re: 3run]
#447147
11/20/14 10:42
11/20/14 10:42
|
Joined: Jun 2014
Posts: 97 Lagos, Nigeria
tolu619
OP
Junior Member
|
OP
Junior Member
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
|
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?
|
|
|
Re: How to extract number from a string?
[Re: 3run]
#447148
11/20/14 10:49
11/20/14 10:49
|
Joined: Jun 2014
Posts: 97 Lagos, Nigeria
tolu619
OP
Junior Member
|
OP
Junior Member
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
|
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:
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.
|
|
|
|