LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Map Variant to Key and values arrays

Solved!
Go to solution

Does anyone know how I could create an array of keys (as a variant) and an array of values (as variants) from a map variant.

 

I would imagine it is possible to do this by parsing the out of variant to flattened string but I currently cannot make sense of the help file for this.

 

Thanks in advance

 

edit: after re-reading I have decided that I haven't been entire clear. I am after:

 

              Input                    Magic code                         Outputs

      Map as a variant                ------->               Array of variants of keys

                                                                          Array of variants of values

0 Kudos
Message 1 of 13
(4,440 Views)

So are you looking for code that takes an arbitrary map and outputs all of the keys and all of the values for that map?

0 Kudos
Message 2 of 13
(4,399 Views)

I'll assume that's what you want 😉

 

Here's some code. The trick is to use a malleable VI (.vim) for the arbitrary map input, not a variant.

 

Decompose map_BD.png

 

Decompose map demo.png

Download All
Message 3 of 13
(4,397 Views)

In my implementation the maps datatype is unkown (It is already a variant) so your implementation isn't quite what I am after.

 

After a day of banging my head against a wall I have worked out a way of doing it though

 

I am open to better ideas as this is incomplete and won't handle all data types atm. 

 

 

0 Kudos
Message 4 of 13
(4,379 Views)

Ahh, I think I get it- your map is already converted to a variant. That's unfortunate.

 

Maybe then could you make a VI that does a "variant data type" to "constant of that datatype"? You'd just have a big case structure with a constant of each type of data you need. Then you could actually convert your map variant into the real map, then use something like my method to get the keys and values.

 

I don't know how that'd work with clusters or arrays though.

0 Kudos
Message 5 of 13
(4,371 Views)
0 Kudos
Message 6 of 13
(4,366 Views)

Somehow it feels like "NI_Data Type.lvlib:Get Map Collection Information.vi" could help, but for some reason (because it's Friday evening) it didn't.

 

Note that the flattened data of a map is exactly the same as the flattened data of an array of clusters with the same types. Variant To Data doesn't accept it though.

0 Kudos
Message 7 of 13
(4,363 Views)

That is what I started out with as well, I found it got more complex when you start to handle strings, paths and arrays where there size is prepended before the data in the flattened string.

0 Kudos
Message 8 of 13
(4,359 Views)

wiebe@CARYA wrote:

Somehow it feels like "NI_Data Type.lvlib:Get Map Collection Information.vi" could help, but for some reason (because it's Friday evening) it didn't.

 

Note that the flattened data of a map is exactly the same as the flattened data of an array of clusters with the same types. Variant To Data doesn't accept it though.


Yeah, i got excited by Get Map Collection Information as well. I had actually built the rest of my code knowing that was there and assuming it would do what I want. Only found out at the last minute that it didn't.

0 Kudos
Message 9 of 13
(4,357 Views)
Solution
Accepted by topic author Worle

@Worle wrote:

That is what I started out with as well, I found it got more complex when you start to handle strings, paths and arrays where there size is prepended before the data in the flattened string.


It still works?! (Seems pretty solid to me?)

Map Variant To Variants capture.png

Map Variant To Variants capture 2.pngMap Variant To Variants capture 3.png

EDIT: Even map values:

Map Variant To Variants capture 4.png

Message 10 of 13
(4,348 Views)