Please enable JavaScript to view this site.

ESL Documentation

Navigation: » No topics above this level «

Q18 Why is the "switch" construction not giving the behaviour I expect?

Scroll Prev Top Next More

The ESL "switch" programming construction is a very powerful tool combining the 'C' multi-conditional "switch" construction together with string pattern matching. The addition of the string pattern matching can lead to unexpected results if you do not fully understand how to make use of its features.

The switch statement syntax in ESL looks like the following:

ESLLan22

where MATCH_CLAUSE is the input to be matched and has the following syntax:  

ESLLan23

In ESL the switch statement performs pattern matching and thus is different to some other languages. When executing a switch statement in ESL, the action statements of the first MATCH_CLAUSE that is contained within the STRING_VALUE supplied will be executed. If no cases match, the default action statements will be executed.

This means that the switch statement is looking for the MATCH_CLAUSE within the STRING_VALUE supplied. Given the following MATCH_CLAUSE:

ESLLan24

any of the following STRING_VALUES would be accepted:

1010

1110101111

10103454

341010

because 1010 appears as a contiguous string within them all. You may further qualify the MATCH_CLAUSE with a starting column position, for example:

ESLLan25

would match,

1010

10103455

because the string 1010 appears as a contiguous string starting with the first character at column position one. If you specify a range, 1 thru 10, then all of the MATCH_CLAUSE characters must appear within the first ten column positions. Thus a MATCH_CLAUSE of,

ESLLan26

would match,

12341010

12341010901

12345610103

 

To avoid an unwanted or ambiguous MATCH_CLAUSE within a switch statement you need to make sure your case statements go from the most specific to the least specific. For example, if you want to test the following numbers; 1, 10, 100, and 1010 you would need to organize your case statements like:

ESLLan27

If you put the case statement for 1 first, this would be the only case ever executed (except for default). This is true since for any of the numbers being tested, one is in the first column position - which is all that case line col "1" is testing for. By reversing the order of the tests, we test the other more specific cases first and only accept the case for one if all the previous cases failed.

This solves our problem to a point. But what happens if we pass 11 into the above switch statement. Since 1010, 100, and 10 are not contained in 11, these cases will fail. But the last case of 1 is contained within 11 and would cause the corresponding action statements to be invoked. In fact we have no control over what characters appear outside of the boundaries specified by the MATCH_CLAUSE.

To solve this problem there are a few choices. The first is to pad all the numbers to the size of the largest string expected. In this way we have control over all the characters within the string. Using this method the above case statements would look like,

ESLLan28

Now if you pass in any number in the same format, every character will be tested explicitly and any chance for ambiguity will be eliminated. To implement this, ESL's string manipulation library (ESLSTR.DLL) contains a PadString function which can pad strings to a given length with a specified character.  

Another method would be to terminate the string with some special character so that all strings would become unique.

ESLLan29

This has the advantage of not requiring the developer to know the maximum length of the string expected.