Author Topic: Parsing Data  (Read 309 times)

Legacy_AmbrosiaPW

  • Jr. Member
  • **
  • Posts: 70
  • Karma: +0/-0
Parsing Data
« on: April 30, 2011, 07:10:15 pm »


               Hello,

I'm not very used to this language. I want to get a number between 1 & 200, then compare it to other numbers I have stored in a list. I'm not really sure how to do that without a lot of code. I'm sure there is an easier way.

This is the way I'm used to doing it.

int iNum = d20(10);
while (iNum [is one of these delimited numbers] 2.42.65.69.103)    //if iNum is one of those numbers
{
iNum = d20(10);                                                                                            //reroll until iNum is not one of the numbers
}


Any suggestions? Thanks!
               
               

               


                     Modifié par AmbrosiaPW, 30 avril 2011 - 06:12 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Parsing Data
« Reply #1 on: April 30, 2011, 07:44:37 pm »


               

int iNum = Random(200)+1;
while(iNum == 2 || iNum == 42 || iNum == 65 || iNum == 69 || iNum == 103)
{
iNum = Random(200)+1;
}

or

int iNum;
do
{
iNum = Random(200)+1;
}while(iNum == 2 || iNum == 42 || iNum == 65 || iNum == 69 || iNum == 103);


               
               

               
            

Legacy_AmbrosiaPW

  • Jr. Member
  • **
  • Posts: 70
  • Karma: +0/-0
Parsing Data
« Reply #2 on: April 30, 2011, 08:29:25 pm »


               Yeah that's the long way I thought of. I thought there might have been a more compact way.

Thank you!
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Parsing Data
« Reply #3 on: April 30, 2011, 08:53:41 pm »


               What shadooow gave is the fastiest way I can see to do it.  it also works great as long as your number set is not dynamic.  If you are building the number set in game them you will need to parse the string as the title implies.  In that case I would add a dilimiter to the begining and end of the string just to simplify the search.  in that case you could use something like this.

  string  sNumSet = ".2.42.65.69.103.";
 
int nNum;
  do nNum = Random(200)+1;
  while (FindSubString(sNumSet, "." +IntToString(nNum)+ "." ) !=-1); 


Edit:  It is inportant to note, that if your number set is static, what you are calling the long way is faster then having to parse the data.   
               
               

               


                     Modifié par Lightfoot8, 30 avril 2011 - 07:56 .
                     
                  


            

Legacy_AmbrosiaPW

  • Jr. Member
  • **
  • Posts: 70
  • Karma: +0/-0
Parsing Data
« Reply #4 on: April 30, 2011, 10:19:39 pm »


               Ok thank you. Yes that is exactly what I was talking about. I have 10 or more that I want to exclude and I didn't like the long comparison. I guess I will just do it the most efficient, yet ugliest, way.