03-12-2007 04:37 AM - edited 03-12-2007 04:37 AM
Have you ever wondered how to implement a set in LabVIEW?
Sets are like arrays with no duplicate values and where order of the elements is ignored. Ordered sets are sets where the elements are sorted in some way. Variant attributes provide a
nice and easy way to implement sets of any LabVIEW datatype.
To implement an ordered set of strings, append each string as a new attribute name to a variant using Set Variant Attribute. There cannot be duplicate attribute names as variant attributes, so LabVIEW automatically guarantees that there are no duplicate strings in the set. To get the elements from the set, use Get Variant Attribute but do not wire the name input. This way you get all the attributes in a set as a alphabetically sorted array.
To implement a set of integer numbers do the same thing, but
this time flatten the integer to a string. To get the values as an ordered
array use Get Variant Attribute, do
not wire anything to name input terminal and convert the values to data using Variant To Data. The integer set is not an ordered set (in the way you would expect) as negative numbers follow positive numbers in internal representation.
You can also implement a set of any data type, not just strings or numbers. The same trick of flattening data to string applies not only to numeric data but to any data.
Last you may sometimes have a need to remove duplicate references. The above method doesn’t work as Variant To Data node doesn’t accept reference data types. However the following piece of code will remove duplicate references.
Tomi
Message Edited by Tomi M on 03-12-2007 12:38 PM
Message Edited by Tomi M on 03-12-2007 12:39 PM
03-12-2007 04:40 AM - edited 03-12-2007 04:40 AM
Message Edited by Tomi M on 03-12-2007 12:40 PM
03-12-2007 09:44 AM
Thank you Tomi!
I will have to experiment with this approach to get my head around what you have done. I have read that the variant has been put "on steroids" since I last looked and the operations are supposed to be much faster.
Has anyone ever compared the new variant performance and how it matches up with non-variant approaches?
Ben
03-12-2007 10:14 AM
03-12-2007 12:33 PM
03-12-2007 01:29 PM
03-12-2007 03:13 PM
Thanks Steve 😉
Seeing as how I didn't know much about this nugget it wasn't too easy nor was it too complicated. I'll say it was just right..![]()
03-12-2007 03:31 PM
03-12-2007 03:49 PM
03-13-2007 05:10 AM