cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
Andyon98
Level II

If I have two lists, how can I associate the nth element of one list to the nth element of the other array?

How can I associate the nth element of one of the lists to the nth element of another list? I was looking into associative arrays but im unsure on how to actually loop through them and associate each key with the values. (Note that my keys would be in one list and the data in the other list).

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: If I have two lists, how can I associate the nth element of one list to the nth element of the other array?

I think you're on the right track; associative arrays would work.  Does the example below help?

Names Default To Here( 1 );

list1={"A","B","C","D"};
list2={5,6,7,8};

aa = Associative Array( list1, list2 );

Show(aa[list1[2]]);

View solution in original post

David_Burnham
Super User (Alumni)

Re: If I have two lists, how can I associate the nth element of one list to the nth element of the other array?

If you are thinking purely in terms of lists then the index associates them i.e. if you want the i'th element of both lists then:

 

value1 = lst1[i];
value2 = lst2[i];

If you want to think in terms of associative arrays then the 2 lists can be used to create an array

arr = AssociativeArray(lst1,lst2);

Then presumably you want to use the value from one list to reference the other e.g.

key = lst1[i];
value = arr[key];

Then to iterate, something like this:

keys = arr << get keys;
for (i=1,i<=nitems(keys),i++,
    key = keys[i];
    value = arr[key];
    show(key,value);
);

For example:

dt = open("$SAMPLE_DATA/Big Class.jmp");

lst1 = dt:name << get values;
lst2 = dt:age << get values;

arr = AssociativeArray(lst1,lst2);

keys = arr << get keys;
for (i=1,i<=nitems(keys),i++,
    key = keys[i];
    value = arr[key];
    show(key,value);
);
-Dave

View solution in original post

5 REPLIES 5

Re: If I have two lists, how can I associate the nth element of one list to the nth element of the other array?

I think you're on the right track; associative arrays would work.  Does the example below help?

Names Default To Here( 1 );

list1={"A","B","C","D"};
list2={5,6,7,8};

aa = Associative Array( list1, list2 );

Show(aa[list1[2]]);
Andyon98
Level II

Re: If I have two lists, how can I associate the nth element of one list to the nth element of the other array?

This helps big time, thank you!

David_Burnham
Super User (Alumni)

Re: If I have two lists, how can I associate the nth element of one list to the nth element of the other array?

If you are thinking purely in terms of lists then the index associates them i.e. if you want the i'th element of both lists then:

 

value1 = lst1[i];
value2 = lst2[i];

If you want to think in terms of associative arrays then the 2 lists can be used to create an array

arr = AssociativeArray(lst1,lst2);

Then presumably you want to use the value from one list to reference the other e.g.

key = lst1[i];
value = arr[key];

Then to iterate, something like this:

keys = arr << get keys;
for (i=1,i<=nitems(keys),i++,
    key = keys[i];
    value = arr[key];
    show(key,value);
);

For example:

dt = open("$SAMPLE_DATA/Big Class.jmp");

lst1 = dt:name << get values;
lst2 = dt:age << get values;

arr = AssociativeArray(lst1,lst2);

keys = arr << get keys;
for (i=1,i<=nitems(keys),i++,
    key = keys[i];
    value = arr[key];
    show(key,value);
);
-Dave
Andyon98
Level II

Re: If I have two lists, how can I associate the nth element of one list to the nth element of the other array?

Hi David,

 

Can you use associative arrays for three parameters? I tried it and it worked for two but the third parameter just created its own list at the end. How can I associate it with each associative element?

David_Burnham
Super User (Alumni)

Re: If I have two lists, how can I associate the nth element of one list to the nth element of the other array?

No you can't, they work as key-value pairs.  I think you need to maintain multiple arrays of there are additional values that you need to track.  I usually end up with a collection of arrays: arrName, arrLSL, arrUSL etc.  Although list elements can be anything, so as an example, with specs I will have an arrangement like this:

specs = arrSpecs[parameter];
lsl = specs["lsl"];
usl = specs["usl"];

i.e. arrSpecs is an array with keys based on parameter name; but the value is another array, with keys "lsl" and "usl".

 

-Dave