Trouble using hashq-get-handle in Guile
Hi Everyone, I'm having trouble getting the value associated with a key in a hash-table and I'm totally confused as to what I'm doing wrong. The code I'm using is shown below: (use-modules (curl)) (use-modules (json)) ;; use curl to hit the site's api to get results of query as json: (define handle (curl-easy-init)) (curl-easy-setopt handle 'url "http://api.data.gov:80/regulations/v3/documents.json?api_key=mySecKey&countsOnly=0&encoded=1&dktid=EPA-HQ-OAR-2013-0602&dkt=R&cp=C&rpp=25&po=0";) (define s (curl-easy-perform handle)) ;; parse the json in the string into guile scheme: (define queryResults (json-string->scm s)) ;; use this to see the keys and values in the resulting hash table: (define print-hash (lambda (my-hash) (hash-for-each (lambda (key value) (format #t "~a => ~a~%" key value)) my-hash))) ;; now let's take a look at the keys and values: (print-hash queryResults) ;; now let's get the value associated with the documents key: (hashq-get-handle queryResults 'documents) It relies on [guile-curl][1] and [guile-json][2] to talk to the webserver and to parse the response. Things seem pretty straight-forward until I run. I can see the response from the server in json (very long and not posted here) and from within Emacs (using geiser to poke around) I can see: scheme@(guile-user)> (hash-table? queryResults) $25 = #t so I know that queryResults is indeed a hash-table. When I take a look at the keys and associated values using print-hash I see that: scheme@(guile-user)> (print-hash queryResults) totalNumRecords => 23318 documents => (# # # # # # # # # # # # # # # # # # # # # # # # #) so that's good and it's what I expected. But when I try to use hashq-get-handle to get a look at the value for totalNumRecords or documents I get: scheme@(guile-user)> (hashq-get-handle queryResults 'totalNumRecords) $24 = #f scheme@(guile-user)> (hashq-get-handle queryResults 'documents) $24 = #f which seems to indicate the key doesn't exist in the hash-table queryResults. I've tried all night with quotes, w/o quotes, futzing with the SRFI-69 hash-table implementations (during which I discovered the difference between native guile hash-tables and the SRFI-69 hash-tables), and went through the [hash table examples][3] in the guile reference manual (they all worked just fine). I'm out of ideas. If someone could help me understand why my calls to hashq-get-handle aren't working out as expected I'd be most grateful. BTW, this question is also posted at: http://stackoverflow.com/questions/27348357/trouble-using-hashq-get-handle-in-guile Thanks for the help in advance. [1]: http://www.lonelycactus.com/guile-curl.html [2]: https://github.com/aconchillo/guile-json [3]: http://www.gnu.org/software/guile/manual/html_node/Hash-Table-Examples.html#Hash-Table-Examples -j -- - Jamil Egdemir uncleja...@gmail.com http://www.power-quant.com (631) 338-3170 (cell) -
Re: Trouble using hashq-get-handle in Guile
() jamil egdemir () Sun, 7 Dec 2014 17:44:46 -0500 (hashq-get-handle queryResults 'documents) IIUC, the hash table object ‘queryResults’ is made by: https://github.com/aconchillo/guile-json/blob/master/json/parser.scm proc ‘read-object’. For interop w/ the ‘make-hash-table’ it calls, you might try some variants of ‘hashq-get-handle’, namely ‘hash-get-handle’ or ‘hashv-get-handle’. -- Thien-Thi Nguyen GPG key: 4C807502 (if you're human and you know it) read my lisp: (responsep (questions 'technical) (not (via 'mailing-list))) => nil signature.asc Description: PGP signature
Re: Trouble using hashq-get-handle in Guile
Thien-Thi, > IIUC, the hash table object ‘queryResults’ is made by: > > https://github.com/aconchillo/guile-json/blob/master/json/parser.scm > > proc ‘read-object’. For interop w/ the ‘make-hash-table’ it calls, you > might try some variants of ‘hashq-get-handle’, namely ‘hash-get-handle’ > or ‘hashv-get-handle’. Thank you for the suggestion. I tried both hash-get-handle and hashv-get-handle: scheme@(guile-user)> (hash-get-handle queryResults 'totalNumRecords) $27 = #f scheme@(guile-user)> (hashv-get-handle queryResults 'totalNumRecords) $28 = #f seems like the result is the same. No love. Good suggestion though... -j -- - Jamil Egdemir uncleja...@gmail.com http://www.power-quant.com (631) 338-3170 (cell) -
Re: Trouble using hashq-get-handle in Guile
jamil egdemir writes: > I'm having trouble getting the value associated with a key in a > hash-table and I'm totally confused as to what I'm doing wrong. The > code I'm using is shown below: > > (use-modules (curl)) > (use-modules (json)) > > ;; use curl to hit the site's api to get results of query as json: > (define handle (curl-easy-init)) > (curl-easy-setopt handle 'url > > "http://api.data.gov:80/regulations/v3/documents.json?api_key=mySecKey&countsOnly=0&encoded=1&dktid=EPA-HQ-OAR-2013-0602&dkt=R&cp=C&rpp=25&po=0";) > (define s (curl-easy-perform handle)) > > ;; parse the json in the string into guile scheme: > (define queryResults (json-string->scm s)) > > ;; use this to see the keys and values in the resulting hash > table: > (define print-hash (lambda (my-hash) > (hash-for-each (lambda (key value) > (format #t "~a => ~a~%" key value)) > my-hash))) > > ;; now let's take a look at the keys and values: > (print-hash queryResults) > > ;; now let's get the value associated with the documents key: > (hashq-get-handle queryResults 'documents) Based on a quick glance at the json code, I suspect you want this: (hash-get-handle queryResults "documents") i.e. change 'hashq-get-handle' to 'hash-get-handle', and pass a string as the key, not a symbol. If you use "~s" instead of "~a" in 'print-hash', it will be clear whether the keys are strings or symbols because strings will print within double quotes. Regards, Mark
Re: Trouble using hashq-get-handle in Guile
Mark, > Based on a quick glance at the json code, I suspect you want this: > > (hash-get-handle queryResults "documents") > > i.e. change 'hashq-get-handle' to 'hash-get-handle', and pass a string > as the key, not a symbol. BOOM! scheme@(guile-user)> (hash-get-handle queryResults "documents") $29 = ("documents" # # # # #\ # # # # # # # # # # # # # # # # # # # #) All good now. > If you use "~s" instead of "~a" in 'print-hash', it will be clear > whether the keys are strings or symbols because strings will print > within double quotes. Excellent suggestion. I'll do that. Thanks to both you and ttn! -j -- - Jamil Egdemir uncleja...@gmail.com http://www.power-quant.com (631) 338-3170 (cell) -
Re: Trouble using hashq-get-handle in Guile
On Sunday, December 7, 2014 4:49 PM, jamil egdemir wrote: >Mark, > >> Based on a quick glance at the json code, I suspect you want this: >> >> (hash-get-handle queryResults "documents") >> >> i.e. change 'hashq-get-handle' to 'hash-get-handle', and pass a string >> as the key, not a symbol. > >BOOM! As an aside, guile 2+ has native functions for fetching via plain HTTP. You shouldn't have to utilize guile-curl unless your query requires special handling: https for example. -Mike