Domanda

Why is my code not properly reading a value in a Switch statement? Code below.

I've verified that it's properly iterating, one character at a time and that the numeric characters appear to match the conditionals. But every character is handled by the default, none by the conditional.

Local StringVar inString := "X12y1023" ;
Local StringVar outString; 
Local NumberVar i :=1; 
...
While i <= Length(inString) 
Do (
   Local StringVar inC := mid(inString, i, 1);
   Local StringVar outC;
   Switch( 
      inC = "1", outC := "!", 
      inC = "2", outC := "Z", 
      inC = "3", outC := "E",
      ... 
      inC = "0", outC := "O", 
      True, outC := inC
     );
   outString := outString + outC;
   i := i+1;
);
outString;

To demonstrate that the number characters are not being read at any point above the default condition (and the length of each iteration is only one character), I modified True as follows:

  True, outC := inC  + "_" + Cstr(Length(inC)) + ", "

Output generates X_1, 1_1, 2_1, y_1, 1_1, 0_1, 2_1, 3_1,

What am I missing?

Thanks

È stato utile?

Soluzione

A switch statement is not really meant to be used the way you're trying to use it; It's meant for returning simple values and not for more complicated statements, like variable assignments. I'd guess that the behavior you're seeing is probably just a byproduct of the specific implementation of the function.

Instead, you can either use the case-statement or rearrange your switch so that it is only used to return simple values.

//Your code changed to use a case-statement
Local StringVar inString := "X12y1023" ;
Local StringVar outString; 
Local NumberVar i :=1; 

While i <= Length(inString) 
Do (
   Local StringVar inC := mid(inString, i, 1);
   Local StringVar outC;
   Select inC
    Case "1" : outC:="!"
    Case "2" : outC:="Z"
    Case "3" : outC:="E"
    Case "0" : outC:="O"
    Default : outC:=inC;
   outString := outString + outC;
   i := i+1;
   );
outString;

Or

//Your code rearranged to use switch as intended
Local StringVar inString := "X12y1023" ;
Local StringVar outString; 
Local NumberVar i :=1; 

While i <= Length(inString) 
Do (
   Local StringVar inC := mid(inString, i, 1);
   outString := outString +
     switch(
      inC = "1", "!",
      inC = "2", "Z",
      inC = "3", "E",
      inC = "0", "O",
      True, inC);
   i := i+1;
   );
outString;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top