LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Scan From String Into Enum -- Exact Match Otherwise Error

Is there any way to accomplish having scan from string, when scanning to an enum, return an error if it's not an exact match? Apparently, if the text in an enum is a subset of the string it will successfully return.

 

ScanFromString.jpg

Message 1 of 12
(4,963 Views)

I just tried this in 2015 SP1 and I get an error if I don't have an exact match. The only difference is my enum isn't a typedef.

0 Kudos
Message 2 of 12
(4,954 Views)

Greg

 

Just tested in LV 2012 and I'm seeing the same as you. If I'm trying to match the enum option "This", then using the string "Thisasdf" does not produce an error, but using "asdfThis" does produce an error. An easy to catch the case you are worried about is to make sure the remaining string is empty.

Snip1.JPG

 

Message 3 of 12
(4,919 Views)

I never noticed this until you pointed it out.  I have LV 2014 and I see the same thing.  The solution is a good one.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 12
(4,909 Views)

Here's another silly way to do it:


s.png

0 Kudos
Message 5 of 12
(4,905 Views)

@Gregory wrote:

Greg

 

Just tested in LV 2012 and I'm seeing the same as you. If I'm trying to match the enum option "This", then using the string "Thisasdf" does not produce an error, but using "asdfThis" does produce an error. An easy to catch the case you are worried about is to make sure the remaining string is empty.

Snip1.JPG

 


I saw the same with regards to when it would match and when it would error (i.e. if the string matches the beginning of the enum item). I should have been more clear. This is actually quite an alarming "feature." I need to track down what happens if you have two items in an enum that would cause a match. Does it always match the first? Is it undetermined? This all came about because I was taking a string I received over TCP/IP and scanning it to an enum in my application layer. One of my coworkers said "I noticed your enum has some items that don't match the documentation" to which I responded "well does the sending side have it wrong too then?"

 

Obviously the answer was no -- there was just no error to be caught. I'm wondering if this is a bug or it is supposed to be that way.

0 Kudos
Message 6 of 12
(4,859 Views)

Hmm, interesting catch.  i think it's designed that way because the primitive can be used to parse a string stream into components, thus parsing what it needs and giving the rest back (it outputs explicity the rest of the string).

Try using %s%[^!-~] as the format specifier.

Never mind, doesn't work.

0 Kudos
Message 7 of 12
(4,846 Views)

I did a quick test, and it correctly distinguished between an enum with the values 'ThisIs' and 'ThisIsnt', the location of the values in the enums lists didn't matter.  LV 2015.

Jon D
Certified LabVIEW Developer.
0 Kudos
Message 8 of 12
(4,832 Views)

@Intaris wrote:

Hmm, interesting catch.  i think it's designed that way because the primitive can be used to parse a string stream into components, thus parsing what it needs and giving the rest back (it outputs explicity the rest of the string).

Try using %s%[^!-~] as the format specifier.

Never mind, doesn't work.


Bummer, was hoping you'd be right!

0 Kudos
Message 9 of 12
(4,807 Views)

What if I meant to type "ThisIsn't" as my string but i missed a couple characters. I'll get a match and it will be for the wrong item, when what I really want is it to match neither item. 

 

Here is the problem: what if I ordered my enum differently so "ThisIsn't" was first. Then scan from string would return that item. Reordering my enum could completely change the functionality of my program. 

 

scan.png

0 Kudos
Message 10 of 12
(4,805 Views)