strstr () in a while-loop



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/04/2003 at 07:11, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   7.303 
    Platform:   Windows  ;   
    Language(s) :   C.O.F.F.E.E  ;

    ---------
    Hi !
    I want to parse a string and therefore seek any ">" in it. But unfortunately, these C.O.F.F.E.E. lines don't work:
    var MyString = "<tag>text</tag>";
    var pos = 0;
    var chpos = 0;
    while (strchr (MyString,"<[0]",pos) != -1){
    chpos = strchr (MyString,"<[0]",pos) ;
    println (tostring(chpos));
    pos = chpos;
    }
    The while loop turns into an infinite loop and the value of pos/chpos remains at 0, which is the first position of "<" in the given string.
    I guess, C.O.F.F.E.E. forgets to update the pos/chpos-variables and this might be caused by using referenced values instead of copies. 
    Does anyone have a clue to solve this ?
    thanx



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/04/2003 at 10:08, xxxxxxxx wrote:

    Why are you searching for "<[0]"?
    It will never find that string in the MyString variable.
    Shouldn't you just be searching for "<"?
    Or, at the very least "<"[0], if this is, at least, valid.
    Also, you said you were looking out for ">" but you never search for it.

    Rui Batista



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/04/2003 at 11:33, xxxxxxxx wrote:

    Sorry, i had to write the code above ex cognito, so it was a bit misleading...
    But my problem remains... here is the corrected code, which still causes an infinite loop instead of printing all occurences of "<"...
    Btw, you have to use "<"[0] to search for "<" with strchr (), because it expects an unicode char... I had the same problem using strstr () instead.....

    var MyString = "<tag>text</tag>";
    var pos = 0;
    var chpos = 0;

    while (strchr (MyString,"<"[0], pos) != -1){
    chpos = strchr (MyString,"<"[0], pos) ;
    println (tostring(chpos));
    pos = chpos;
    }
    I appreciate your help a LOT !
    thanx....



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/04/2003 at 11:47, xxxxxxxx wrote:

    Just use this:

      
    while (strchr (MyString,"<"[0], pos) != -1){   
    chpos = strchr (MyString,"<"[0], pos) ;   
    println (chpos);   
    pos = chpos+1;   
    }  
    

    You were starting the search always from the same place, hence the infinite loop. With pos=chpos+1; you will start on the next character.
    Also, you don't need the tostring to print chpos.
    Was this what you needed?

    Rui Batista



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/04/2003 at 13:13, xxxxxxxx wrote:

    Thanx a lot Rui...
    That was really a stupid mistake, but after having written some dozen lines of code, one can hardly free his mind to eliminate such errors...


Log in to reply