AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   String Help? (https://forums.alliedmods.net/showthread.php?t=65232)

Drak 01-04-2008 03:07

String Help?
 
I have a variable that holds a string value like so:
Code:
 1.5.15.64.34
It's filled with random numbers, separated by a period. How would I check if the variable has number "1" or "15" inside of it?

Arkshine 01-04-2008 03:39

Re: String Help?
 
contain() ?

Drak 01-04-2008 04:12

Re: String Help?
 
Quote:

Originally Posted by arkshine (Post 569913)
contain() ?

Already thought of that.. But.. What if I checked if it contained the number "1".
Wouldn't "15" register? Meaning it contain would return true?

Alka 01-04-2008 04:32

Re: String Help?
 
Simple, just strtok the string then loop through arrays and find if contain or not the wanted number. :wink:

Tell me if you want a little demonstration :p

Drak 01-04-2008 04:35

Re: String Help?
 
Quote:

Originally Posted by Alka (Post 569931)
Simple, just strtok the string then loop through arrays and find if contain or not the wanted number. :wink:

Tell me if you want a little demonstration :p

Demonstration please. D:
I already tried using 'strtok' and failed horribly.

Alka 01-04-2008 04:48

Re: String Help?
 
Here...i've tested it, and works, but one little thing...read the comment at String[] :s

Code:

public plugin_cfg() //You can use your_func, i've used this for debug. :)
{
 //Size 10, length 10 x).
 new Array[10][10];
 
 //Hmm, some odd thing, you must have an "." after last
 //number to store it in array. Shity strotk.
 //Or you can let it without "." at the end, and check the Sttring too after array.
 new String[] = "1.5.15.64.34.";
 
 new Num;
 while(containi(String, ".") != -1)
 {
  strtok(String, Array[Num], sizeof Array[] - 1, String, sizeof String - 1, '.');
 
  Num++;
 }
 
 for(new i = 0 ; i < sizeof Array ; i++)
 {
  //Magic skip.
  if(!Array[i][0])
  continue;
 
  //Degug thing.
  server_print("DEBUG: Array[%d] = %d", i, str_to_num(Array[i]));
 
  //This is the magic trick.
  if(containi(Array[i], "15") != -1) //Can use equal/i too. :p
  server_print("DEBUG: Array[%d] matched with value %d", i, str_to_num(Array[i]));
 }
}


Drak 01-04-2008 05:15

Re: String Help?
 
Quote:

Originally Posted by Alka (Post 569938)
Here...i've tested it, and works, but one little thing...read the comment at String[] :s

Code:

public plugin_cfg() //You can use your_func, i've used this for debug. :)
{
 //Size 10, length 10 x).
 new Array[10][10];
 
 //Hmm, some odd thing, you must have an "." after last
 //number to store it in array. Shity strotk.
 //Or you can let it without "." at the end, and check the Sttring too after array.
 new String[] = "1.5.15.64.34.";
 
 new Num;
 while(containi(String, ".") != -1)
 {
  strtok(String, Array[Num], sizeof Array[] - 1, String, sizeof String - 1, '.');
 
  Num++;
 }
 
 for(new i = 0 ; i < sizeof Array ; i++)
 {
  //Magic skip.
  if(!Array[i][0])
  continue;
 
  //Degug thing.
  server_print("DEBUG: Array[%d] = %d", i, str_to_num(Array[i]));
 
  //This is the magic trick.
  if(containi(Array[i], "15") != -1) //Can use equal/i too. :p
  server_print("DEBUG: Array[%d] matched with value %d", i, str_to_num(Array[i]));
 }
}


Sweet, thanks.
Though.. I ran into a small problem. I commented the problem:
Code:
bool:CheckAchievement(id,achievement) {     new Num,Array[ACHVS_NUM][ACHVS_NUM],String[64] // Right Here.     copy(String,63,g_Achvs[id]) // I have the string in a global variable. Do I need to copy it over?         new iAchievement[11]     num_to_str(achievement,iAchievement,10);         while(containi(g_Achvs[id],".") != -1)     {         strtok(String,Array[Num],sizeof(Array[]) - 1,String,sizeof(String) - 1,'.');         Num++     }     for(new i;i<sizeof(Array);i++)     {         if(!Array[i][0])             continue                 if(containi(Array[i],iAchievement) != -1)             return true     }     return false }

Running the current code, throws a "Index out of bounds" error.

EDIT: Also. "ACHVS_NUM" is defined as 10

Alka 01-04-2008 05:38

Re: String Help?
 
Nah, you don't need to copy it into String[] that was just an e.g , in your case will be something like...

Code:

public bool:CheckAchievement(id,achievement)
{
 new Num, Array[ACHVS_NUM][ACHVS_NUM];
 
 new iAchievement[11]
 num_to_str(achievement, iAchievement, 10);
 
 while(containi(g_Achvs[id],".") != -1)
 {
  strtok(g_Achvs[id], Array[Num], sizeof(Array[]) - 1, g_Achvs[id], sizeof(g_Achvs[]) - 1, '.');
  Num++;
 }
 for(new i ; i < sizeof(Array) ; i++)
 {
  if(!Array[i][0])
  continue;
 
  if(containi(Array[i], iAchievement) != -1)
  return true;
 }
 return false;
}

EDIT:I hope that g_Achvs is a string... "g_Achvs[33][length]" :p

Drak 01-04-2008 05:57

Re: String Help?
 
Quote:

Originally Posted by Alka (Post 569952)
Nah, you don't need to copy it into String[] that was just an e.g , in your case will be something like...
*CODE*
EDIT:I hope that g_Achvs is a string... "g_Achvs[33][length]" :p

Almost there. D: But I keep getting "Index Out of Bounds" error on the 'strtok' line.

Also, Yeah, that's correct.
Code:
new g_Achvs[33][128]
Thanks again, Alka.

Arkshine 01-04-2008 05:58

Re: String Help?
 
I would do something like :

Code:
    my_function()     {         new my_string[] = ".1.5.15.64.34"; // the first charac. must be a '.'                 if( is_contain( my_string, 15 ) )             //         }         bool:is_contain( source[], num )     {         new i = strlen( source );                 while( i-- >= 0 )         {             if( i == -1 || source[ i ] != '.' )                 continue;                             if( num == str_to_num( source[ i + 1 ] ) )                 return true;                             source[ i ] = '^0';         }         return false;     }


All times are GMT -4. The time now is 11:13.

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