10-11-2021 09:43 AM
Is there a way to preserve the Insertion Order of the Elements in a map collection. Currently map sorts the elements inserted alphabetically?
10-11-2021 10:23 AM
@Kimar wrote:
Is there a way to preserve the Insertion Order of the Elements in a map collection.
No.
You'd have to use two arrays... Maybe wrapped in a class.
@Kimar wrote:
Currently map sorts the elements inserted alphabetically?
The logic is probably based on > and = (or >=). So for text, that is pretty much alphabetically.
10-11-2021 11:43 AM
@Kimar wrote:
Is there a way to preserve the Insertion Order of the Elements in a map collection. Currently map sorts the elements inserted alphabetically?
This statement is incorrect. Maps are not "sorted", but are internally maintained as a higher data structure (e.g. balanced binary tree) in memory to optimize operations (lookup, insertion, deletion, etc.) If you extract all elements using a FOR loop, they are necessarily sorted by key. That's the way it works. You are welcome to sort them any other way you want later, of course.
Can you explain what you are trying to do? You could use something else as others have suggested, or you could make a more advanced map, e.g. where the insertion order is part of the value.
10-11-2021 05:20 PM
I am trying store a set of commands I need to send to instruments in an order. I am using instrument handle as the key and 1 D array command/value as the value as shown in the example below. when I read the all the keys to execute each command in a sequence, they are not in the same order as inserted.
key--> value
1. Instr1-->RST
2. Instr1-->CLS
3. Instr2-->RST
4. Instr2-->CLS
5. Instr2-->Select Ch 1
6. Instr2-->Set Voltage 2.0
7. Instr1-->Measure Voltage
I created an advanced map that takes the index as key the another map as value and seems to work. I am trying to see if there is an easy way to handle this.
Thanks
10-11-2021 08:50 PM
If you insert those key->value pairs into a map, you will only end up with 2 entries (the last for each key). Maps do not preserve order, so they are not the correct datatype to use. Arrays do preserve order. You can sort an array of clusters where the first element(s) will decide the sort order. Plenty of examples of doing that in the forums here.
10-13-2021 07:03 AM
@Kimar wrote:
I am trying store a set of commands I need to send to instruments in an order. I am using instrument handle as the key and 1 D array command/value as the value as shown in the example below. when I read the all the keys to execute each command in a sequence, they are not in the same order as inserted.
key--> value
1. Instr1-->RST
2. Instr1-->CLS
3. Instr2-->RST
4. Instr2-->CLS
5. Instr2-->Select Ch 1
6. Instr2-->Set Voltage 2.0
7. Instr1-->Measure Voltage
I created an advanced map that takes the index as key the another map as value and seems to work. I am trying to see if there is an easy way to handle this.
Thanks
Seems like you need a map with Instrument as key, and array as type.
So after adding the values, you get:
1. Instr1-->RST, CLS, Measure Voltage
3. Instr2-->RST CLS, Select Ch 1, Set Voltage 2.0
I would probably consider a Command class instead, with for each command a child., and a Commands child containing an array of Commands. Each Instrument would contain (or use) it's own Commands object.
10-13-2021 07:11 AM
I think I would also go with an array of clusters. The cluster would contain the instrument, the command, and the data. The array will preserve the order and a cluster is a simple way to hold all of the parameters together.
10-13-2021 11:50 AM
@altenbach wrote:
@Kimar wrote:
Is there a way to preserve the Insertion Order of the Elements in a map collection. Currently map sorts the elements inserted alphabetically?
This statement is incorrect. Maps are not "sorted", but are internally maintained as a higher data structure (e.g. balanced binary tree) in memory to optimize operations (lookup, insertion, deletion, etc.).
I think that's semantics.
The elements do have an order relative to each other. This order is maintained in the tree. This relative position of the elements does result in an alphabetical order when you get them.
I'd say maps are sorted, because they "internally maintained as a higher data structure (e.g. balanced binary tree) in memory to optimize operations (lookup, insertion, deletion, etc.)."
12-08-2021 06:07 AM
Hi!
can you please share me that vi. since i was new to lab view trying to write commands to read the voltages from the device .can you please help me in this,like how can we read the voltages of the device through RS232.
12-08-2021 06:31 AM
@manisaipavan wrote:
Hi!
can you please share me that vi. since i was new to lab view trying to write commands to read the voltages from the device .can you please help me in this,like how can we read the voltages of the device through RS232.
This seems to be posted in the wrong thread?