sym and lookup problem

Q&A's, tips, howto's
Locked
jeremyc
Posts: 33
Joined: Wed Dec 06, 2006 3:33 am

sym and lookup problem

Post by jeremyc »

I have the following function:

Code: Select all

(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:

Code: Select all

(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:

Code: Select all

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

Code: Select all

(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

Code: Select all

(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:

Code: Select all

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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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

Code: Select all

(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...

Locked