LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

"Reverse Lookup" using Variants?

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!

0 Kudos
Message 1 of 23
(3,453 Views)

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.

Message 2 of 23
(3,445 Views)

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

 

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 3 of 23
(3,441 Views)

Hi abvenk,

 

What should happen if two keys have the same value?

0 Kudos
Message 4 of 23
(3,434 Views)

Hi gregoryj,

 

My data would not have duplicates. Each name-value pair is unique.

0 Kudos
Message 5 of 23
(3,429 Views)

There are VIs in the Data Type Parsing palette that might help you.  (LV 2015+, I believe.)

 

Edit: I think I misunderstood you.  Sorry.

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 6 of 23
(3,428 Views)

This will be build in soon (LV19?): Associative-Arrays

Message 7 of 23
(3,359 Views)

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

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 23
(3,356 Views)

@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...

0 Kudos
Message 9 of 23
(3,351 Views)

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

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 10 of 23
(3,336 Views)