cancel
Showing results for
Show  only  | Search instead for
Did you mean:
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.

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

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.

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.