A lookup table is a table of values that may need to be indiscriminately retrieved programmatically, often even determined subjectively by a user’s input. Using an array and brute force linear searching is often the first method that comes to mind for many programmers, but the larger the table is or grows the more inefficient this method quickly becomes. Variant lookup tables can be created using a simple LabVIEW API that utilizes very efficient algorithms for key-value pairs.
A hierarchical variant lookup table goes a step further and establishes tables within tables. This allows a programmer to efficiently organize their tables of key-value pairs.
While variant lookup tables are one of the most effective tools a developer has at their disposal, too often it goes mistakenly forgotten in favor of using arrays. The larger amount of data lookups you’re preforming, however, the more ideal a variant lookup will be.
Variant lookup tables in the average case have a complexity of O(log N) and in the worst case O(N). This nontrivially outperforms using linear searches on arrays which have a O(N) complexity, especially in the case of larger datasets.
So why do variant tables go forgotten in cases they’d increase performance or even simplify code? It’s likely that programmers find the concept of variants a bit more foreign to their array counterparts. Once introduced and utilized by developers, however, the variant lookup table can become a best friend. Variant lookup tables allow for clear hierarchical distinctions using subtables as demonstrated below.
An uncommon use case for variant lookup tables, for example, is for control references when running multiple asynchronous VI’s. Take a case where an application has a tab control with multiple pages and subpanels. Often in such applications, references to controls within those subpanels are needed to update information on the main page. There may be many controls and indicators on each subpanel, and so it be ineffective to continuously search an array for the correct control name each time. It be much more effective to use a variant lookup table to pass control references during the initialization of each subpanel VI. Here below a variant lookup table is passed to each asynchronous VI along with a notifier.
Which is then used in each subpanel VI called to notify when the references have been built. Notice a hierarchy is used to group the control references.
For context, the numbers in each following subpanel are being randomly generated.
Once a Variant Lookup table is established, it is easy then, to refer to controls in the main VI using Get Variant Attribute.vi, like so:
Remembering to use variant lookup tables, can not only improve the performance of your code, but organize it as well. This makes what can be complex ordeals much simpler.
Since a hierarchy has been established, you can also use the same label names for in your subpanels. Very convenient! Don’t forget to consider inviting your best friend next time you’re developing!