LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Map collection

Is there a way to preserve the Insertion Order of the Elements in a map collection. Currently map sorts the elements inserted alphabetically?

0 Kudos
Message 1 of 10
(2,230 Views)

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

0 Kudos
Message 2 of 10
(2,212 Views)

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

0 Kudos
Message 3 of 10
(2,191 Views)

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

0 Kudos
Message 4 of 10
(2,166 Views)

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.

0 Kudos
Message 5 of 10
(2,150 Views)

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

0 Kudos
Message 6 of 10
(2,078 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 7 of 10
(2,074 Views)

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

0 Kudos
Message 8 of 10
(2,057 Views)

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.

0 Kudos
Message 9 of 10
(1,961 Views)

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

0 Kudos
Message 10 of 10
(1,958 Views)