LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Mapping a 2D string array into Array of Objects

I currently have several 2D arrays of strings:

For example:

LastName FirstName Age
Johnson Stephen 34
Smith Lucy 36
Clark Kevin 42

and I have created a class to represent it:

For example:
class Person { LastName: string, FirstName: string, Age: integer }

 

I am looking for a way to map that table to an array of objects of that class.

 

For example:

[{LastName: Johnson, FirstName: Stephen, Age: 34}, {LastName: Smith, FirstName: Lucy, Age: 36}, {LastName: Clark, FirstName: Kevin, Age: 42}]

 

Ideally i'm looking for a way to do this where it could be a generic VI so I could supply the class and the data (2D array) and it would output a list of those objects. 

 

Is there a simple way of doing this in LabVIEW?

 

 

0 Kudos
Message 1 of 6
(1,386 Views)

The quick answer to your question is not really. You are trying to apply text based programming techniques to a graphical programming language. Python is an interpretted language and can do this type of thing. LabVIEW, specifically the G language, is a tightly type checked language which makes what you want to do much more challenging.

 

There are ways you can create an array of classes given the input data but it would not be very generalized. There are a few ways you could make it more generic but it would still require knowing the specifics of the class at some point. You could define a base class with a generic method that would accept an array input. The inheritied classes would then implement the method of parsing/interpretting the generic input and linitializing the cclass data. This is however a very kludgy implementation and is not very robost. I would approach this by implementing well defined classes and create a parser and a class factory which would create your individual objects for the specfied data.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 6
(1,358 Views)

Thank you for the reply.

 

I typically work with C# which is also strongly typed; however, there are many ways to use things such as reflection to get names and whatnot for parsing. 

 

I am using a factory which I planned to retrieve the table and build out the list of objects. However what I was looking for was a way to make it smart about how it builds out the objects. I could go by hand and just use my knowledge of the table to tell it to put column x into property y as it iterates over each row; however, it would be nice if it could use the column name to choose which property to insert the values into. Ideally I want the structure of the table to be indepent of how it is parsed. In addition by being generic it means I don't have to write/draw almost identical code for each factory. 

 

I know there are simple parsers build into LabVIEW for XML and JSON so I was curious if there was one for 2D tables of information. 

0 Kudos
Message 3 of 6
(1,351 Views)

There are someways using references to look at the names or captions of controls which might allow you to do some generic processing. However, your column names would have to match the internal name/caption of the controls/indicators. You could have an external mapping table which could link the column name with the internal control name/caption.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 6
(1,346 Views)

There are a few hurdles to get this done. Probably not worth the effort.

 

Getting the types from the class's private data. That is not easy. You can use either Variants, XML, JSON or flattened data. The problem is that if a class is 'default', the XML, JSON or flattened data will not be there. Getting variant data from the class isn't easy either, but doable with VI server.

 

The text to type conversion. You'd need to implement this for every type you want to support, including recursion for cluster and array types. Note that even if you don't want to convert types from string to text, you might still want the get meaningful results if the class has unsupported data types.

 

Getting the data from the entire hierarchy. If you want to support that... Personally, I see little point in making this if it doesn't support inheritance.  This isn't too difficult.

 

Putting the string data back into the class's private data. This is again not easy at all. Going from variant elements to a cluster, and adding flattened data so it can be converted to a class is very, very tricky. It's mostly solvable, but there are rusty edges (like variant attributes, and variant data containing types that can't be distinguished from the types converted to variant).

 

I probably forgot to mention a few 😊.

 

Unless this saves weeks on your development, I'd simply give each class a "From 2D Array Of Strings" and "To 2D Array Of Strings" method. Optionally inherited from an interface.

 

Of course this means that you do need to adjust these methods every time you change the class. There are middle grounds. For instance, give each supported class a labeled cluster. Parsing only clusters is a lot easier then parsing classes, maybe this will even fit in a malleable VI, but you'd still need to implement this in each supported class.

0 Kudos
Message 5 of 6
(1,319 Views)

@PhillipAC wrote:

I know there are simple parsers build into LabVIEW for XML and JSON so I was curious if there was one for 2D tables of information. 


Going from a 2D table to anything accepted by 'the parsing' is the least problem...

0 Kudos
Message 6 of 6
(1,318 Views)