cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
%3CLINGO-SUB%20id%3D%22lingo-sub-236892%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%BE%AA%E7%92%B0%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E7%9A%84%E5%85%83%E7%B4%A0%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236892%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CDIV%20class%3D%22lia-message-template-problem-zone%22%3E%E5%95%8F%E9%A1%8C%3CP%3E%E6%82%A8%E9%9C%80%E8%A6%81%E6%9F%A5%E7%9C%8B%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E4%B8%AD%E7%9A%84%E6%AF%8F%E5%80%8B%E5%85%83%E7%B4%A0%3C%2FP%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%A1%88%3CP%3E%E4%BD%BF%E7%94%A8%20%26lt%3B%3CFIRST%20%E5%92%8C%3D%22%22%3E%26lt%3B%3CNEXT%20%E6%B6%88%E6%81%AF%3D%22%22%3E%3C%2FNEXT%3E%3C%2FFIRST%3E%3C%2FP%3E%3CBR%20%2F%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%2F%2F%20load%20a%20table%20of%20elements%0Adt%20%3D%20Open(%20%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FList_of_chemical_elements%22%2C%20HTML%20Table(%204%2C%20Column%20Names(%202%20)%2C%20Data%20Starts(%205%20)%20)%20)%3B%0A%2F%2F%20make%20an%20associative%20array%20using%20%22H%22%20to%20lookup%20%22Hydrogen%22%20and%20some%20of%20its%20properties%0Amap_Symbol_To_Data%20%3D%20Associative%20Array(%20dt%5B1%20%3A%3A%20118%2C%20%22Symbol%22%5D%2C%20dt%5B1%20%3A%3A%20118%2C%20%5B3%2F*element%20name*%2F%2C%201%2F*element%20number*%2F%2C%207%2F*atomic%20weight*%2F%5D%5D%20)%3B%0A%2F%2F%20close%20the%20table%0AClose(%20dt%2C%20nosave%20)%3B%0A%0A%2F%2F%20loop%20through%20the%20keys%20using%20%26lt%3B%3CFIRST%20and%3D%22%22%3E%26lt%3B%3CNEXT%20for%3D%22%22%3E%3C%2FNEXT%3E%3C%2FFIRST%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3E%E8%A8%8E%E8%AB%96%3CP%3E%E4%B8%8A%E9%9D%A2%E7%9A%84%E4%B8%AD%E9%96%93%E5%A1%8A%E6%BC%94%E7%A4%BA%E4%BA%86%20aa%26lt%3B%3CFIRST%20%E5%92%8C%3D%22%22%20aa%3D%22%22%3E%26lt%3B%3CNEXT%20%E6%B6%88%E6%81%AF%E4%BB%A5%E5%8F%8A%3D%22%22%20isempty%3D%22%22%20%E6%B8%AC%E8%A9%A6%3D%22%22%3E%3C%2FNEXT%3E%3C%2FFIRST%3E%3C%2FP%3E%3CP%3E%26lt%3B%3CFIRST%3E%3CBR%20%2F%3E%E4%B9%9F%E5%8F%AF%E4%BB%A5%E7%9C%8B%E7%9C%8B%3C%2FFIRST%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2F14-2%2Fcreate-associative-arrays.shtml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2F14-2%2Fcreate-associative-arrays.shtml%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-440312%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E5%BE%AA%E7%92%B0%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E7%9A%84%E5%85%83%E7%B4%A0%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-440312%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E8%AC%9D%E8%AC%9D%EF%BC%81%3C%2FP%3E%0A%3CP%3E%E9%80%99%E8%AE%93%E6%88%91%E6%83%B3%E7%9F%A5%E9%81%93%E7%82%BA%E4%BB%80%E9%BA%BC%E8%A6%81%E6%94%B9%E8%AE%8A%EF%BC%9F%20%E7%B6%AD%E5%9F%BA%E7%99%BE%E7%A7%91%E6%9C%89%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fw%2Findex.php%3Ftitle%3DList_of_chemical_elements%26amp%3Baction%3Dhistory%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%E5%81%89%E5%A4%A7%E7%9A%84%E6%AD%B7%E5%8F%B2%3C%2FA%3E%EF%BC%8C%E7%9C%8B%E8%B5%B7%E4%BE%86%E5%83%8F%20~2021%20%E5%B9%B4%206%20%E6%9C%88%2030%20%E6%97%A5%E3%80%82%20%E5%AE%83%E9%82%84%E5%85%B7%E6%9C%89%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fw%2Findex.php%3Ftitle%3DList_of_chemical_elements%26amp%3Boldid%3D1031067113%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%E8%88%8A%E9%A0%81%3C%2FA%3E%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%82%A8%E6%83%B3%E7%9F%A5%E9%81%93%E5%AE%83%E4%BB%A5%E5%89%8D%E6%98%AF%E4%BB%80%E9%BA%BC%E6%A8%A3%E5%AD%90%EF%BC%8C%E5%AE%83%E4%BB%8D%E7%84%B6%E6%9C%89%E6%95%88%E3%80%82%3C%2FP%3E%0A%3CP%3E%E5%8E%9F%E5%A7%8B%E4%BB%A3%E7%A2%BC%E4%BD%BF%E7%94%A8%E4%BA%86%E5%88%97%E8%99%9F%E5%92%8C%E5%88%97%E5%90%8D%E7%A8%B1%E7%9A%84%E6%B7%B7%E5%90%88%E5%8C%B9%E9%85%8D%E3%80%82%20%E6%88%91%E6%83%B3%E9%80%99%E6%9C%89%E5%A5%BD%E6%9C%89%E5%A3%9E%E3%80%82%20%E8%87%B3%E5%B0%91%E6%8C%87%E5%AE%9A%E7%9A%84%E5%88%97%E5%A4%B1%E6%95%97%E4%BA%86%EF%BC%8C%E5%84%98%E7%AE%A1%E5%AE%83%E4%BD%9C%E7%82%BA%E4%B8%80%E5%80%8B%E6%95%B8%E5%AD%97%E5%B0%B1%E5%8F%AF%E4%BB%A5%E4%BA%86%E3%80%82%20%E4%BD%86%E6%98%AF%EF%BC%8C%E7%AC%AC%207%20%E5%88%97%E6%88%96%E7%AC%AC%208%20%E5%88%97%E5%8F%AF%E8%83%BD%E6%9C%83%E9%BB%98%E9%BB%98%E5%9C%B0%E5%A4%B1%E6%95%97%2F%E4%BD%BF%E7%94%A8%E4%BE%86%E8%87%AA%E9%8C%AF%E8%AA%A4%E5%88%97%E7%9A%84%E9%8C%AF%E8%AA%A4%E5%8E%9F%E5%AD%90%E9%87%8F%E3%80%82%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-440300%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E5%BE%AA%E7%92%B0%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E7%9A%84%E5%85%83%E7%B4%A0%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-440300%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%E8%AC%9D%E8%AC%9D%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F982%22%20target%3D%22_blank%22%3E%40Craige_Hales%3C%2FA%3E%E9%80%B2%E8%A1%8C%E6%9C%89%E7%94%A8%E7%9A%84%E6%BC%94%E7%A4%BA%E3%80%82%3C%2FP%3E%0A%3CP%3E%E7%95%B6%E5%98%97%E8%A9%A6%E9%80%99%E5%80%8B%E8%85%B3%E6%9C%AC%E6%99%82%EF%BC%8C%E6%88%91%E6%B3%A8%E6%84%8F%E5%88%B0%E7%B6%AD%E5%9F%BA%E7%99%BE%E7%A7%91%E5%B7%B2%E7%B6%93%E6%9B%B4%E6%96%B0%E4%BA%86%E8%A1%A8%E6%A0%BC%EF%BC%8C%E4%BD%BF%E5%BE%97%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E5%91%BD%E4%BB%A4%E8%BF%94%E5%9B%9E%E9%8C%AF%E8%AA%A4%E3%80%82%3C%2FP%3E%0A%3CP%3E%E6%88%91%E8%AA%8D%E7%82%BA%E5%A6%82%E6%9E%9C%E8%A6%81%E6%AD%A3%E7%A2%BA%E7%94%9F%E6%88%90%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%EF%BC%8C%E5%89%87%E9%9C%80%E8%A6%81%E9%81%8B%E8%A1%8C%E6%AD%A4%E8%A1%8C%E3%80%82%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Emap_Symbol_To_Data%20%3D%20Associative%20Array(%20dt%5B1%20%3A%3A%20118%2C%20%22Element%202%22%5D%2C%20dt%5B1%20%3A%3A%20118%2C%20%5B3%2F*element%20name*%2F%2C%201%2F*element%20number*%2F%2C%208%2F*atomic%20weight*%2F%5D%5D%20)%3B%0A%26nbsp%3B%26nbsp%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-381699%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E5%BE%AA%E7%92%B0%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E7%9A%84%E5%85%83%E7%B4%A0%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-381699%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Esym%20%3D%20map_Symbol_To_Data%20%26lt%3B%26lt%3B%20first%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%E6%AA%A2%E7%B4%A2%E7%AC%AC%E4%B8%80%E5%80%8B%E5%AF%86%E9%91%B0%E3%80%82%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Esym%20%3D%20map_Symbol_To_Data%20%26lt%3B%26lt%3B%20next(%20sym%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%E6%AA%A2%E7%B4%A2%E7%95%B6%E5%89%8D%E9%8D%B5%E8%BC%B8%E5%85%A5%E5%BE%8C%E7%9A%84%E9%8D%B5%3CEM%3E%E7%AC%A6%E8%99%9F%3C%2FEM%3E%E3%80%82%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%E5%B0%8D%E6%96%BC%20jsl%20%E8%AE%8A%E9%87%8F%E3%80%81%E5%87%BD%E6%95%B8%E6%88%96%E6%96%B9%E6%B3%95%E4%BE%86%E8%AA%AA%EF%BC%8C%E5%A4%A7%E5%B0%8F%E5%AF%AB%E4%B8%A6%E4%B8%8D%E9%87%8D%E8%A6%81%E3%80%82%20%E7%84%B6%E8%80%8C%EF%BC%8C%E9%80%99%E5%B0%8D%E6%96%BC%E5%AF%A6%E9%9A%9B%E7%9A%84%E9%8D%B5%E5%80%BC%E5%BE%88%E9%87%8D%E8%A6%81%E3%80%82%20sym%3D%22A%22%20%E5%92%8C%20sym%3D%22a%22%20%E6%98%AF%E5%85%A9%E5%80%8B%E4%B8%8D%E5%90%8C%E7%9A%84%E9%8D%B5%E3%80%82%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Emap_Symbol_To_Data%5Bsym%5D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%E5%9C%A8%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E4%B8%AD%E6%9F%A5%E6%89%BE%20sym%20%E5%80%BC%E3%80%82%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Emap_symbol_to_data%20%E6%87%89%E8%A9%B2%E6%8C%87%E7%A4%BA%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E6%98%AF%E7%AC%A6%E8%99%9F%EF%BC%88sym%EF%BC%8C%E9%8D%B5%EF%BC%89%E5%88%B0%E6%95%B8%E6%93%9A%E5%80%BC%E7%9A%84%E6%98%A0%E5%B0%84%E3%80%82%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-381693%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E5%BE%AA%E7%92%B0%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E7%9A%84%E5%85%83%E7%B4%A0%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-381693%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E7%AC%AC%E4%B8%80%E5%80%8B%E6%96%B9%E6%B3%95%EF%BC%88First%E3%80%81Next%EF%BC%89%E6%98%AF%E5%90%A6%E5%BE%AA%E7%92%B0%E9%81%8D%E6%AD%B7%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E7%9A%84%E9%8D%B5%E6%88%96%E5%80%BC%EF%BC%9F%20%E5%8F%A6%E5%A4%96%EF%BC%8C%E5%9C%A8%E8%85%B3%E6%9C%AC%E7%B4%A2%E5%BC%95%E4%B8%AD%EF%BC%8C%E5%AD%98%E5%9C%A8%E6%8B%BC%E5%AF%AB%E4%B8%8D%E4%B8%80%E8%87%B4%E7%9A%84%E5%95%8F%E9%A1%8C%E2%80%94%E2%80%94%E4%B8%80%E9%96%8B%E5%A7%8BFirst%E6%B2%92%E6%9C%89%E5%A4%A7%E5%AF%AB%EF%BC%8C%E7%84%B6%E5%BE%8C%E6%98%AF%E4%B8%8B%E9%9D%A2%E7%9A%84%EF%BC%8C%E6%89%80%E4%BB%A5%E6%88%91%E4%B8%8D%E7%9F%A5%E9%81%93%E6%98%AF%E5%90%A6%E6%87%89%E8%A9%B2%E5%A4%A7%E5%AF%AB%E3%80%82%20%E6%87%89%E8%A9%B2%E6%98%AF%E5%97%8E%EF%BC%9F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-237918%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E5%BE%AA%E7%92%B0%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E7%9A%84%E5%85%83%E7%B4%A0%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-237918%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E5%B7%A7%E5%A6%99%E5%9C%B0%E4%BD%BF%E7%94%A8for%E5%BE%AA%E7%92%B0%E3%80%82%E6%88%91%E4%B9%8B%E5%89%8D%E5%8F%AA%E4%BD%BF%E7%94%A8%E9%81%8E%20while%20%E8%88%87%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E3%80%82%E6%88%91%E5%BE%97%E8%A9%A6%E8%A9%A6%E9%80%99%E5%80%8B%E3%80%82%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Looping through an Associative Array's elements
Problem

You need to look at every element in an Associative Array

Solution

use the <<First and <<Next messages


// load a table of elements
dt = Open( "https://en.wikipedia.org/wiki/List_of_chemical_elements", HTML Table( 4, Column Names( 2 ), Data Starts( 5 ) ) );
// make an associative array using "H" to lookup "Hydrogen" and some of its properties
map_Symbol_To_Data = Associative Array( dt[1 :: 118, "Symbol"], dt[1 :: 118, [3/*element name*/, 1/*element number*/, 7/*atomic weight*/]] );
// close the table
Close( dt, nosave );

// loop through the keys using <<FIRST and <<NEXT
For( sym = map_Symbol_To_Data << first, !Is Empty( sym ), sym = map_Symbol_To_Data << next( sym ),
	// print the symbol and look up the related data 
	Write( "\!n", sym, " ", map_Symbol_To_Data[sym] )
);

// another way to loop through them; this requires duplicating the data
keys = map_Symbol_To_Data << getkeys;
values = map_Symbol_To_Data << getvalues;
// loop through the pair of lists using an index
For( ikey = 1, ikey <= N Items( keys ), ikey += 1,
	sym = keys[ikey];
	Write( "\!n", sym, " ", values[ikey] );
	// also, you don't have to use a list of values, 
	// you could use the key to look it up:
	Write( "\!n", sym, " ", map_Symbol_To_Data[sym] );
);
Discussion

The middle block above demonstrates the aa<<First and aa<<Next messages, and the IsEmpty test. Sending the aa<<First message to the associative array object returns the first key. The aa<<Next(prev key) returns the next key in the sequence.

The <<first, <<next technique can streamline the JSL a lot by not introducing unneeded variables. The third block introduces three extra variables and some copying. Creating the two lists may make sense if you need them anyway or will use them several times. The lookups in the associative array are slower than indexing into the list.


See Also

https://www.jmp.com/support/help/14-2/create-associative-arrays.shtml 

 

Comments
johnmoore

Clever use of a for loop.  I have only used  while's with associative arrays before.  I will have to try this.

mostarr

Does the first method (First, Next) loop through the keys or the values of the associative array? Also, in the Scripting Index, there is a spelling inconsistency--at first First is not capitalized, then it is lower down, so I don't know if it should be capitalized or not. Should it be?

sym = map_Symbol_To_Data << first

retrieves the first key.

sym = map_Symbol_To_Data << next( sym )

retrieves the key after the current key in sym.

 

Capitalization is not important for jsl variables, functions, or methods. It is important for the actual key value however. sym="A" and sym="a" are two distinct keys.

 

map_Symbol_To_Data[sym]

looks up the sym value in the associative array.

 

map_symbol_to_data is supposed to indicate the associative array is a mapping of symbols (sym, the keys) to data values.

 

 

ron_horne

 

Thank you @Craige_Hales  for the useful demonstration.

When trying this script i have noticed that Wikipedia has updated the table such that the associative array command returns an error.

I think this is the line that needs to be run if for the associative array to be produced correctly.

 

map_Symbol_To_Data = Associative Array( dt[1 :: 118, "Element 2"], dt[1 :: 118, [3/*element name*/, 1/*element number*/, 8/*atomic weight*/]] );
  

 

Thanks!

which left me wondering, why the change? Wikipedia has great history, looks like ~30jun2021. It also has the old page, which still works if you wonder what it used to look like.

The original code used a mix'n'match of column numbers and column names. That's good and bad, I guess. At least the named column failed, though it would have been fine as a number. But then column 7 or 8 might have been silently failing/working with a bad atomic weight from the wrong column.

 

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.