cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-236892%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EDurchlaufen%20der%20Elemente%20eines%20assoziativen%20Arrays%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%3EProblem%3CP%3ESie%20m%C3%BCssen%20jedes%20Element%20in%20einem%20assoziativen%20Array%20betrachten%3C%2FP%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3EL%C3%B6sung%3CP%3EVerwenden%20Sie%20die%20Nachrichten%20%26lt%3B%3CFIRST%20und%3D%22%22%3E%26lt%3B%3CNEXT%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%3EDiskussion%3CP%3EDer%20mittlere%20Block%20oben%20demonstriert%20die%20Nachrichten%20%E2%80%9Eaa%26lt%3B%3CFIRST%3E%3C%2FFIRST%3E%3C%2FP%3E%3CP%3EDie%20%26lt%3B%3CFIRST%3E%3CBR%20%2F%3ESiehe%20auch%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%3EBetreff%3A%20Durchlaufen%20der%20Elemente%20eines%20assoziativen%20Arrays%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-440312%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EDanke!%3C%2FP%3E%0A%3CP%3EDa%20fragte%20ich%20mich%3A%20Warum%20die%20%C3%84nderung%3F%20Wikipedia%20hat%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%20tolle%20Geschichte%3C%2FA%3E%20%2C%20sieht%20aus%20wie%20~30.06.2021.%20Es%20hat%20auch%20die%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%20alte%20Seite%3C%2FA%3E%20%2C%20was%20immer%20noch%20funktioniert%2C%20wenn%20Sie%20sich%20fragen%2C%20wie%20es%20fr%C3%BCher%20aussah.%3C%2FP%3E%0A%3CP%3EDer%20urspr%C3%BCngliche%20Code%20verwendete%20eine%20Mischung%20aus%20Spaltennummern%20und%20Spaltennamen.%20Das%20ist%20gut%20und%20schlecht%2C%20denke%20ich.%20Zumindest%20ist%20die%20benannte%20Spalte%20fehlgeschlagen%2C%20obwohl%20sie%20als%20Zahl%20in%20Ordnung%20gewesen%20w%C3%A4re.%20Aber%20dann%20k%C3%B6nnte%20Spalte%207%20oder%208%20stillschweigend%20ausgefallen%20sein%20bzw.%20mit%20einem%20schlechten%20Atomgewicht%20aus%20der%20falschen%20Spalte%20funktioniert%20haben.%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%3EBetreff%3A%20Durchlaufen%20der%20Elemente%20eines%20assoziativen%20Arrays%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%3EDanke%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F982%22%20target%3D%22_blank%22%3E%20%40Craige_Hales%3C%2FA%3E%20f%C3%BCr%20die%20n%C3%BCtzliche%20Demonstration.%3C%2FP%3E%0A%3CP%3EBeim%20Ausprobieren%20dieses%20Skripts%20ist%20mir%20aufgefallen%2C%20dass%20Wikipedia%20die%20Tabelle%20so%20aktualisiert%20hat%2C%20dass%20der%20Befehl%20%E2%80%9EAssoziatives%20Array%E2%80%9C%20einen%20Fehler%20zur%C3%BCckgibt.%3C%2FP%3E%0A%3CP%3EIch%20denke%2C%20dass%20dies%20die%20Zeile%20ist%2C%20die%20ausgef%C3%BChrt%20werden%20muss%2C%20damit%20das%20assoziative%20Array%20korrekt%20erstellt%20wird.%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%3EBetreff%3A%20Durchlaufen%20der%20Elemente%20eines%20assoziativen%20Arrays%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%3Eruft%20den%20ersten%20Schl%C3%BCssel%20ab.%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%3ERuft%20den%20Schl%C3%BCssel%20nach%20der%20aktuellen%20Eingabe%20ab%3CEM%3E%20sym%3C%2FEM%3E%20.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDie%20Gro%C3%9F-%20und%20Kleinschreibung%20ist%20f%C3%BCr%20JSL-Variablen%2C%20-Funktionen%20oder%20-Methoden%20nicht%20wichtig.%20Es%20ist%20jedoch%20wichtig%20f%C3%BCr%20den%20tats%C3%A4chlichen%20Schl%C3%BCsselwert.%20sym%3D%22A%22%20und%20sym%3D%22a%22%20sind%20zwei%20unterschiedliche%20Schl%C3%BCssel.%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%3Esucht%20den%20Sym-Wert%20im%20assoziativen%20Array.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%E2%80%9Emap_symbol_to_data%E2%80%9C%20soll%20anzeigen%2C%20dass%20es%20sich%20bei%20dem%20assoziativen%20Array%20um%20eine%20Zuordnung%20von%20Symbolen%20(sym%2C%20den%20Schl%C3%BCsseln)%20zu%20Datenwerten%20handelt.%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%3EBetreff%3A%20Durchlaufen%20der%20Elemente%20eines%20assoziativen%20Arrays%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-381693%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EDurchl%C3%A4uft%20die%20erste%20Methode%20(First%2C%20Next)%20eine%20Schleife%20durch%20die%20Schl%C3%BCssel%20oder%20die%20Werte%20des%20assoziativen%20Arrays%3F%20Au%C3%9Ferdem%20gibt%20es%20im%20Skriptindex%20eine%20Rechtschreibunstimmigkeit%3A%20Zuerst%20wird%20%E2%80%9EFirst%E2%80%9C%20nicht%20gro%C3%9Fgeschrieben%2C%20dann%20steht%20es%20weiter%20unten%2C%20sodass%20ich%20nicht%20wei%C3%9F%2C%20ob%20es%20gro%C3%9Fgeschrieben%20werden%20soll%20oder%20nicht.%20Sollte%20es%20sein%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-237918%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EBetreff%3A%20Durchlaufen%20der%20Elemente%20eines%20assoziativen%20Arrays%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-237918%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EClevere%20Verwendung%20einer%20for-Schleife.Ich%20habe%20bisher%20nur%20whiles%20mit%20assoziativen%20Arrays%20verwendet.Ich%20werde%20das%20versuchen%20m%C3%BCssen.%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.