# gets the next element (key, value) from the array #OP NEXT =(REF AARRAY array )REF AAELEMENT:BEGINWHILE( curr position OF array IS nil element list )AND curr hash OF array <UPB( elements OF array )DO# reached the end of the current element list - try the next # curr hash OF array +:= 1; curr position OF array :=( elements OF array )[ curr hash OF array ]OD;IF curr hash OF array >UPB( elements OF array )THEN# no more elements # nil elementELIF curr position OF array IS nil element listTHEN# reached the end of the table # nil elementELSE# have another element #REF AAELEMENTLIST found element = curr position OF array; curr position OF array := next OF curr position OF array; element OF found elementFIEND;# NEXT #

Associative arrays in App Inventor are lists of key:value 'pairs'.
When a list is organized as pairs, the lookup in pairs block can be used to retrieve an associated value from a key name.<VIEW BLOCKS AND ANDROID APP>

To iterate over a map, in the primary sense, use the overmap utility. We will copy the map (cp operator) so as not to modify the original:

births cp dup {1 +} overmap !

To see the results, use the valmap operator:

valmap ! lsnum !

Output:

( 1918 1733 1883 1810 )

There are many ways to interact with a map in Babel. Most of these begin by converting the map to a list or list-of-lists. To look up a list of specific values from the map, by key, use the lumapls utility:

Property lists are lists of alternating keys and values, where each value's key is the element of the list immediately following it. Printing could be done with standard mapping functions, but loop's destructuring makes things a bit easier.

In E, the basic iteration protocol and syntax work over key-value pairs. Therefore, any iteration over a map or other collection is always key-value, though the user may choose to ignore the keys or the values.

The for loop takes either one pattern, for the value, or two, for the key and value; for iterating over keys alone the value may be given an ignore-pattern (_).

\ Written in ANS-Forth; tested under VFX.\ Requires the novice package: http://www.forth.org/novice.html\ The following should already be done:\ include novice.4th\ include association.4th

\ I would define high-level languages as those that allow programs to be written without explicit iteration. Iteration is a major source of bugs.\ The example from the FFL library doesn't hide iteration, whereas this example from the novice-package does.

marker AssociationIteration.4th

\ ******\ ****** The following defines a node in an association (each node is derived from ELEMENT).\ ******

# Be careful when using each(), however, because it uses a global iterator# associated with the hash. If you call keys() or values() on the hash in the# middle of the loop, the each() iterator will be reset to the beginning. If# you call each() on the hash somewhere in the middle of the loop, it will# skip over elements for the "outer" each(). Only use each() if you are sure# that the code inside the loop will not call keys(), values(), or each().while(my($k,$v)=each%pairs){print"(k,v) = ($k, $v)\n";}

do j=1forwords(stateL)/*show all capitols that were defined. */ q=word(stateL, j)/*get the next (USA) state in the list.*/say'the former capitol of ('q") "left(stateN.q, w)" was " stateC.qend/*j*//* [↑] show states that were defined.*/exit/*stick a fork in it, we're all done. *//*──────────────────────────────────────────────────────────────────────────────────────*/setSC: parsearg code,name,cap; upper code /*get code, name & cap.; uppercase code*/ stateL=stateL code /*keep a list of all the US state codes*/ stateN.code=name; w=max(w,length(name))/*define the state's name; max width. */ stateC.code=cap /* " " " code to the capitol*/return/*return to invoker, SETSC is finished.*/

output

the former capitol of (AL) Alabama was Tuscaloosa
the former capitol of (CA) California was Benicia
the former capitol of (CO) Colorado was Denver City
the former capitol of (CT) Connecticut was Hartford and New Haven (jointly)
the former capitol of (DE) Delaware was New-Castle
the former capitol of (GA) Georgia was Milledgeville
the former capitol of (IL) Illinois was Vandalia
the former capitol of (IN) Indiana was Corydon
the former capitol of (IA) Iowa was Iowa City
the former capitol of (LA) Louisiana was New Orleans
the former capitol of (ME) Maine was Portland
the former capitol of (MI) Michigan was Detroit
the former capitol of (MS) Mississippi was Natchez
the former capitol of (MO) Missouri was Saint Charles
the former capitol of (MT) Montana was Virginia City
the former capitol of (NE) Nebraska was Lancaster
the former capitol of (NH) New Hampshire was Exeter
the former capitol of (NY) New York was New York
the former capitol of (NC) North Carolina was Fayetteville
the former capitol of (OH) Ohio was Chillicothe
the former capitol of (OK) Oklahoma was Guthrie
the former capitol of (PA) Pennsylvania was Lancaster
the former capitol of (SC) South Carolina was Charlestown
the former capitol of (TN) Tennessee was Murfreesboro
the former capitol of (VT) Vermont was Windsor

When this example was started, the intention was to list the former capitals by key. Unfortunately, there's a duplicate (Lancaster).

// to iterate over identifiers of a list one needs to use the function ''members''// the identifiers are returned as a lexicographically ordered string row-vector// here ["!", "hello", "world"]for(i in members(x)) { printf("%s %g\n", i, x.[i]); }

// occasionally one needs to check if there exists member of a listy = members(x); // y contains ["!", "hello", "world"]clear(x.["!"]); // remove member with identifier "!" from the list "x"for(i in y) { printf("%s %g\n", i, x.[i]); } // this produces error because x.["!"] does not exist

for(i in y) { if (exist(x.[i])) { printf("%s %g\n", i, x.[i]); } // we print a member of the list "x" only if it exists}

;; Iterate over the table, passing the key and the value of each entry;; as arguments to a function:(hash-table-for-each table;; Create by "partial application" a function that accepts 2 arguments,;; the key and the value:(pa$ format #t "Key = ~a, Value = ~a\n"))

Output:

Key = !, Value = 999
Key = world, Value = 22
Key = hello, Value = 0

;; Iterate over the table and create a list of the keys and the;; altered values:(hash-table-map table(lambda(key val)(list key (+ val 5000))))

;; Create a new table that has the same keys but altered values.(use gauche.collection)(map-to <hash-table>(lambda(k-v)(cons(car k-v)(+(cdr k-v)5000))) table)

To get a list of the keys or of the values of the table,
use one of the following: