Show how to iterate over the key-value pairs of an associative array, and print each pair out.

Also show how to iterate just over the keys, or the values, if there is a separate way to do that in your language.



See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


      REM Store some values with their keys:
      PROCputdict(mydict$, "FF0000", "red")
      PROCputdict(mydict$, "00FF00", "green")
      PROCputdict(mydict$, "0000FF", "blue")
 
      REM Iterate through the dictionary:
      i% = 1
      REPEAT
        i% = FNdict(mydict$, i%, v$, k$)
        PRINT v$, k$
      UNTIL i% = 0
      END
 
      DEF PROCputdict(RETURN dict$, value$, key$)
      IF dict$ = "" dict$ = CHR$(0)
      dict$ += key$ + CHR$(1) + value$ + CHR$(0)
      ENDPROC
 
      DEF FNdict(dict$, I%, RETURN value$, RETURN key$)
      LOCAL J%, K%
      J% = INSTR(dict$, CHR$(1), I%)
      K% = INSTR(dict$, CHR$(0), J%)
      value$ = MID$(dict$, I%+1, J%-I%-1)
      key$ = MID$(dict$, J%+1, K%-J%-1)
      IF K% >= LEN(dict$) THEN K% = 0
      = K%