11-09-2018 12:06 PM
Hello all,
I am using LabVIEW 2014. I'm trying to implement a "reverse lookup" using variants. The names are strings, and values are 1D DBL arrays.
Typically, the Get Variant Attribute takes a name input and outputs the corresponding value. There is a serious limitation to this. I cannot input a value and get the corresponding name. In this case, if I have an array input, I cannot just wire it to the Get Variant Attribute and get the corresponding name string. I need this "reverse lookup" for my application.
Please let me know if I'm missing something, or if someone has figured out a neat trick to make this happen.
Thanks!
11-09-2018 12:26 PM
Short of just getting every variant attribute and looping over all the elements to find your matching array, you will need some sort of secondary data structure to do the reverse mapping. I would probably put the arrays in DVRs, and use a second look up table that stores the array name as the attribute, and the DVR number as the key.
11-09-2018 12:31 PM - edited 11-09-2018 12:32 PM
I'd probably just populate 2 distinct variants with reversed roles for the attribute name and value.
1. This would depend on each of the DBL arrays containing a unique set of values. No duplicates!
2. I'd turn the DBL array into an attribute name using "Flatten to String".
3. Take a moment and consider whether you might want to reverse the string before using it as a unique name. The "red black tree" storage structure works more efficiently when you can resolve uniqueness closer to the beginning of the name string. When you flatten an array, you'll get an optional 4 bytes for string size followed by a definite 4 bytes for array size followed by 8 bytes per element. There's reason to suspect those leading bytes not to vary much, so perhaps you should use the reversed string as the unique name.
I've used the "Flatten to String" and reversal idea before in some of my own code that used variants as a lookup table. It works.
-Kevin P
11-09-2018 12:33 PM - edited 11-09-2018 12:34 PM
Hi abvenk,
What should happen if two keys have the same value?
11-09-2018 12:40 PM
Hi gregoryj,
My data would not have duplicates. Each name-value pair is unique.
11-09-2018 12:41 PM - edited 11-09-2018 12:46 PM
There are VIs in the Data Type Parsing palette that might help you. (LV 2015+, I believe.)
Edit: I think I misunderstood you. Sorry.
11-12-2018 03:27 AM
This will be build in soon (LV19?): Associative-Arrays
11-12-2018 03:49 AM
That should be easy, just do a Number to String and use as Name and use the name as Value in the Variant Attribute.
/Y
11-12-2018 04:56 AM
@Yamaeda wrote:
That should be easy, just do a Number to String and use as Name and use the name as Value in the Variant Attribute.
/Y
Pretty much what Kevin said, right? If you need it two ways, just apply the same trick twice.
I like his advice on reversing the string. Have to test that sometime...
11-12-2018 07:30 AM
wiebe@CARYA wrote:
@Yamaeda wrote:
That should be easy, just do a Number to String and use as Name and use the name as Value in the Variant Attribute.
/Y
Pretty much what Kevin said, right? If you need it two ways, just apply the same trick twice.
I like his advice on reversing the string. Have to test that sometime...
wiebe@CARYA wrote:
@Yamaeda wrote:
That should be easy, just do a Number to String and use as Name and use the name as Value in the Variant Attribute.
/Y
Pretty much what Kevin said, right? If you need it two ways, just apply the same trick twice.
I like his advice on reversing the string. Have to test that sometime...
Pretty much, yes. I didn't read his answer too meticulus. The reverse string is a good idea.
/Y