Looping through an Associative Array's elementsProblem
You need to look at every element in an Associative Array
Solution
use the <<
// 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=""></NEXT></FIRST>
Re: Looping through an Associative Array's elementsThanks!
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.
Re: Looping through an Associative Array's elements
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*/]] );
Re: Looping through an Associative Array's elementssym = 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.
Re: Looping through an Associative Array's elementsDoes 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?
Re: Looping through an Associative Array's elementsClever use of a for loop. I have only used while's with associative arrays before. I will have to try this.