Arc Forumnew | comments | leaders | submitlogin
How do I iterate through an object?
1 point by Thr4wn 5321 days ago | 1 comment
Maybe I'm just oblivious, but I can't figure out how to iterate through an object. eg...

(foreach key o (prn (o key)))

or something like that. forlen seems only to count from 0 to (- (len o) 1) and so I can't just do (o key).

How do I iterate over an object at all? Do I have to always have

(forlen n ob (let key ((keys ob) n) ... ))

b/c if so, then I feel like surely this is a common enough task that at least anarki has a macro for it.



3 points by thaddeus 5321 days ago | link

There are many options - here are a few:

  arc> (= myhashtable (obj 1 "One" 2 "Two"))
  #hash((1 . "One") (2 . "Two"))

  1.
  arc> (each key (keys myhashtable) 
         (prn "key " key " val " myhashtable.key))

  key 2 val Two
  key 1 val One

  2. 
  arc> (each (k v) myhashtable 
            (prn "key " k " val " v))
  key 2 val Two
  key 1 val One
  #hash((1 . "One") (2 . "Two"))

  3.
  arc> (each item myhashtable 
         (prn item))

  (2 Two)
  (1 One)
  #hash((1 . "One") (2 . "Two"))
There's a bunch more documented here:

http://files.arcfn.com/doc/iteration.html

-----