sym and lookup problem

Started by jeremyc, March 10, 2007, 08:21:29 AM

Previous topic - Next topic

jeremyc

I have the following function:



(define (rowAsHash res idx)
  (set 'h '())
  (dotimes (fidx (fieldCount res))
  (push (list (sym (fieldName res fidx)) (value res idx fidx)) h))
  h)


fieldName returns a string. value returns the contents of the particular row/field index combo. Basically, I do:



(pg:query db "SELECT id, code FROM users"
  (lambda (rec) ;; rec = result of rowAsHash
    (println "Id: " (lookup 'id rec) " code: " (lookup 'code rec))))


Now, what happens is my id and code print's are nil. If I do a (println rec) then I get:



((id 10) (code "jdoe"))


Which looks OK. Now, if I remove the (sym ... ) around the (fieldName ...) portion of my rowAsHash function, therefore, I have:



(define (rowAsHash res idx)
  (set 'h '())
  (dotimes (fidx (fieldCount res))
  (push (list (fieldName res fidx) (value res idx fidx)) h))
  h)


I can do this



(pg:query db "SELECT id, code FROM users"
  (lambda (rec) ;; rec = result of rowAsHash
    (println "Id: " (lookup "id" rec) " code: " (lookup "code" rec))))


And that works as expected. My output is:



Id: 10 code: jdoe


Does anyone have an idea as to what I am doing wrong with the first example? Also, is it possible that I could have symbol collisions, say, if someone query's a table with the field name, "name"? Thus using the reserved symbol 'name ?



Thanks,



Jeremy

cormullion

#1
It's puzzling me. If you reduce your code to the simplest form, the output is what you'd expect:


(dotimes (fidx 10)
      (push (list (sym "id") (rand 10)) h -1)
      (push (list (sym "code") "jdoe") h -1))

(println h)

(println
(lookup 'id h) " " (lookup 'code h)
)

((id 3)
 (code "jdoe")
 (id 6)
 (code "jdoe")
 (id 7)
 (code "jdoe")
 (id 5)
 (code "jdoe")
 (id 3)
 (code "jdoe")
 (id 5)
 (code "jdoe")
 (id 6)
 (code "jdoe")
 (id 2)
 (code "jdoe")
 (id 9)
 (code "jdoe")
 (id 1)
 (code "jdoe"))
 
3 jdoe


So obviously your expanded code is doing something different... Perhaps a more experienced eye can spot something I can't see...