Simple While loop problem

vmailvmail Junior MemberPosts: 11
Hi have a simple while loop and I don't know what's wrong, I want the contents of the Line variable spilt by commas to be placed in a structure.
Line = ",105,103,125,117,102,116,126,113,112,119,108,109,110,118"
WHILE(LENGTH_STRING(Line) > 2){
    Sources.ButtonNo[count] = ATOI(REMOVE_STRING(Line, ',', 1))
    count=count+1
}

The loop never stops, If I clear the contents of the Line variable in debug then the loop stops and the structure is populated.

Comments

  • ericmedleyericmedley Senior Member - 3709 Posts Posts: 4,141
    My guess is your specification that the length of the var after removing the "," is greater than 2 chars. Debug can fool you when you're viewing the value of a char variable. You may see only one or even zero chars in the value window but in fact there are a couple chars left like $A,$D or whatnot. (Chars that are non-ascii and don't showup but are there just the same) You can doulbe check this by switching the view from ASCII to HEX. Or even easier yet - the length of the string is shown as well.

    Another thing to bear in mind is that when you hit the last number, there is no longer a comma to remove. So "Line" will be stuck at 3 chars long. You might want to put the whole evaluation inside an if(find_string(Line,',',1)) statement. That way when you're on the last number you'll know because the find_string:comma will fail. you could do something like this:
    
    if(find_stirng(Line,',',1){
      Sources.ButtonNo[count] = ATOI(REMOVE_STRING(Line, ',', 1))
      count=count+1
    } // if find_string
    else if(length_string(Line))){ // last char
      Sources.ButtonNo[count] = ATOI(Line);
      Line='' /// clear out last number - making Line length zero and killing the loop
      }// else
    
    
  • vmailvmail Junior Member Posts: 11

    Thank you.
  • vmailvmail Junior Member Posts: 11
    It's like REMOVE_STRING (on line 2) is not updated til you are out of the while loop. EG, On the third line I'm looking for "Obs Cabin2" which does not exist in the file, the main while loop (line 1) keeps going.
    WHILE(LENGTH_STRING(SOURCESDATA > 0)){
        Line = REMOVE_STRING(SOURCESDATA, CRLF, 1)
        If(FIND_STRING(Line, 'Obs Cabin2' , 1)){
            count=1
            WHILE(LENGTH_STRING(Line) > 0){
                If(FIND_STRING(Line, ',' , 1)){
                    Sources.Enable[count] = ATOI(REMOVE_STRING(Line, ',', 1))
                    count=count+1
                }Else{ // last char
                    Sources.Enable[count] = ATOI(Line);
                    Line='' /// clear out last number - making Line length zero and killing the loop
                }
            }
            Break
        }
    }
    
  • Joe HebertJoe Hebert Junior Member Posts: 2,154
    vmail wrote: »
    It's like REMOVE_STRING (on line 2) is not updated til you are out of the while loop. EG, On the third line I'm looking for "Obs Cabin2" which does not exist in the file, the main while loop (line 1) keeps going.
    WHILE(LENGTH_STRING(SOURCESDATA > 0)){
    Line = REMOVE_STRING(SOURCESDATA, CRLF, 1)
    If(FIND_STRING(Line, 'Obs Cabin2' , 1)){
    count=1
    WHILE(LENGTH_STRING(Line) > 0){
    If(FIND_STRING(Line, ',' , 1)){
    Sources.Enable[count] = ATOI(REMOVE_STRING(Line, ',', 1))
    count=count+1
    }Else{ // last char
    Sources.Enable[count] = ATOI(Line);
    Line='' /// clear out last number - making Line length zero and killing the loop
    }
    }
    Break
    }
    }
    

    The function REMOVE_STRING() is not the problem here. Your *logic* is the culprit.

    Line 1:
    WHILE(LENGTH_STRING(SOURCESDATA > 0)){
    
    evaluates to TRUE if anything is in SOURCEDATA


    Line 3:
    If(FIND_STRING(Line, 'Obs Cabin2' , 1)){
    
    evaluates to TRUE if Obs Cabin2 exists in the Line variable.


    Why should a FALSE for Line 3 automatically assume Line 1 is FALSE? They have nothing to do with each other.
  • viningvining X Member Posts: 4,346
    If your delimiter is always CRLF I would use while(find_string(source,crlf,1)) line = remove the same. Likewise in your next while if that delimeter is , I would do the same while find string, remove string type of approach. You must make sure your loops exit so test a while for one thing and then doing something else with out an escape hatch is dangerous.
  • vmailvmail Junior Member Posts: 11
    Joe Hebert wrote: »

    The function REMOVE_STRING() is not the problem here. Your *logic* is the culprit.

    Line 1:
    WHILE(LENGTH_STRING(SOURCESDATA > 0)){
    
    evaluates to TRUE if anything is in SOURCEDATA


    Line 3:
    If(FIND_STRING(Line, 'Obs Cabin2' , 1)){
    
    evaluates to TRUE if Obs Cabin2 exists in the Line variable.


    Why should a FALSE for Line 3 automatically assume Line 1 is FALSE? They have nothing to do with each other.

    Hi, Line 2 should be removing one line at a time from SOURCESDATA, when all lines have gone then line 1 should return FALSE.
  • vmailvmail Junior Member Posts: 11
    vining wrote: »
    If your delimiter is always CRLF I would use while(find_string(source,crlf,1)) line = remove the same. Likewise in your next while if that delimeter is , I would do the same while find string, remove string type of approach. You must make sure your loops exit so test a while for one thing and then doing something else with out an escape hatch is dangerous.

    Thanks....
Sign In or Register to comment.