"0".."9","9"

 A case structure with "0".."9","9"

"0".."9","9" ... Looks kind of dumb, right?
 
This is the kind of thing that if you know it, it is boring. If you haven't seen it before, you might learn something.
 
Why not just write "0".."9"?
 

Let's give that a go. This is what I get after clicking 'run'. 


Oops. So "9" is not "0".."9". But "9" is "0".."9", "9". That's not what I was expecting, anyway!


So why is this the case? Well, if the datatype was numeric, then the range 0..9 includes the digits 0 and 9. However, with strings, the range is calculated on the ascii value, and crucially the range is not inclusive of the upper value. There are a few threads out there such as this one which discuss whether it is a bug.

 

I actually ran into this issue when doing the MLUG Coding Challenges.* One challenge was to sort a table in alphabetical order from final letter to first letter of each word  - i.e. reverse the strings so they are backwards before sorting alphabetically. One of the columns was an age column, and subsequently I wanted to do some zero padding of any numbers until they were all the same length, but I couldn't detect any 9s! This bit of code didn't make it to my final solution, but did help me learn something.

 

Sidenote: ranges in case structures are an awesome feature that I really didn't utilise enough when I started LabVIEW. It's great being able to specify string ranges, even if you do have to use "a".."z", "z" for the lowercase alphabet.


That's all for now - I'd be interested to hear in the comments if you've come across this issue before!


Cheers,


Leah

 

*If you're looking for some fun challenges, have a go (1,2) then compare with what the MLUG crowd came up with.


Comments