player2_dz

Untitled

Oct 6th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. p2_strInStr = {
  2.     _needle = _this select 0;               //needle input      "wo"
  3.     _haystack = _this select 1;             //haystack input    "hello world"
  4.     _needle = strToArray(_needle);          //converts to: ["w","o"];
  5.     _haystack = strToArray(_haystack);      //converts to: ["h","e","l","l","o"," ","w","o","r","l","d"];
  6.     _needleLength = count (_needle);        //converts to: 2
  7.     _foundCount = 0;                        //used later
  8.     _foundPos = 0;                          //^
  9.     _return = 0;                            //output for later
  10.     _continue = true;                       //used later
  11.  
  12.     //for each item in the haystack, each letter in hello world...
  13.     {
  14.         //if the first letter of the needle (w), matches any of the characters in the haystack, then run some code...
  15.         if (_haystack select _foundPos == _needle select 0) then {
  16.             //firstly, add 1 to foundCount...we'll use it later
  17.             _foundCount = 1;
  18.             //this tells it to continue searching the haystack for other matching chars
  19.             _continue = true;
  20.             //while the length of wo, aka 2, is greater than the amount of letters its found we need to keep searching the haystack for the rest of the matching characters
  21.             //we also check if continue is still true
  22.             while ((_needleLength ) > _foundCount && _continue == true) {
  23.                 if (_haystack select (_foundPos + 1)) then {
  24.                     _foundCount = _foundCount + 1; //stops the loop if it finds all the letters
  25.                     _continue = true;             // keeps the loop going if it hasnt found them all
  26.                 } else {
  27.                     _foundCount = 0;               //reset foundCount
  28.                     _continue = false;              //end the loop, so it continues to the next letter in the haystack
  29.                 };
  30.             };
  31.  
  32.             _foundPos = _foundPos + 1;          //continue to next letter in haystack once loop has ended
  33.         };
  34.  
  35.     } count _haystack;
  36.  
  37.     //once its done, if foundcount is the same as needle length then it did find the full length of wo in hello world.
  38.     if (_foundCount == (_needleLength -1)) then {
  39.         _return = _foundPos;    //set the return output to the position in hello world that it found wo
  40.     } else {
  41.         _return = 0;            //if foundcount not same as needle lenght, string was not found, return 0, a failure.
  42.     };
  43.    
  44.     _return
  45. };
Advertisement
Add Comment
Please, Sign In to add comment