* Trouble using hashq-get-handle in Guile
@ 2014-12-07 22:44 jamil egdemir
2014-12-07 23:27 ` Thien-Thi Nguyen
2014-12-08 0:38 ` Mark H Weaver
0 siblings, 2 replies; 6+ messages in thread
From: jamil egdemir @ 2014-12-07 22:44 UTC (permalink / raw)
To: guile-user
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 => (#<hash-table 2ad7c60 17/31> #<hash-table 2afdbc0
17/31> #<hash-table 2b0ab60 17/31> #<hash-table 2b0eb00 17/31>
#<hash-table 2b10aa0 17/31> #<hash-table 2b13a40 17/31> #<hash-table
2b189e0 17/31> #<hash-table 2b1b980 17/31> #<hash-table 2b1e920 17/31>
#<hash-table 2b228c0 17/31> #<hash-table 2b25860 17/31> #<hash-table
2b29800 17/31> #<hash-table 2b2d7a0 17/31> #<hash-table 2b30740 17/31>
#<hash-table 2b336e0 17/31> #<hash-table 2b38680 17/31> #<hash-table
2b3b620 17/31> #<hash-table 2b3f5c0 17/31> #<hash-table 2b44560 17/31>
#<hash-table 2b48500 17/31> #<hash-table 2b4c4a0 17/31> #<hash-table
2b50440 17/31> #<hash-table 2b533e0 17/31> #<hash-table 2b57380 17/31>
#<hash-table 2b5c320 17/31>)
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
unclejamil@gmail.com
http://www.power-quant.com
(631) 338-3170 (cell)
-------------------------------------------------------------
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Trouble using hashq-get-handle in Guile
2014-12-07 22:44 Trouble using hashq-get-handle in Guile jamil egdemir
@ 2014-12-07 23:27 ` Thien-Thi Nguyen
2014-12-07 23:34 ` jamil egdemir
2014-12-08 0:38 ` Mark H Weaver
1 sibling, 1 reply; 6+ messages in thread
From: Thien-Thi Nguyen @ 2014-12-07 23:27 UTC (permalink / raw)
To: guile-user
[-- Attachment #1: Type: text/plain, Size: 684 bytes --]
() jamil egdemir <unclejamil@gmail.com>
() 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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 197 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Trouble using hashq-get-handle in Guile
2014-12-07 23:27 ` Thien-Thi Nguyen
@ 2014-12-07 23:34 ` jamil egdemir
0 siblings, 0 replies; 6+ messages in thread
From: jamil egdemir @ 2014-12-07 23:34 UTC (permalink / raw)
To: guile-user
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
unclejamil@gmail.com
http://www.power-quant.com
(631) 338-3170 (cell)
-------------------------------------------------------------
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Trouble using hashq-get-handle in Guile
2014-12-07 22:44 Trouble using hashq-get-handle in Guile jamil egdemir
2014-12-07 23:27 ` Thien-Thi Nguyen
@ 2014-12-08 0:38 ` Mark H Weaver
2014-12-08 0:49 ` jamil egdemir
1 sibling, 1 reply; 6+ messages in thread
From: Mark H Weaver @ 2014-12-08 0:38 UTC (permalink / raw)
To: jamil egdemir; +Cc: guile-user
jamil egdemir <unclejamil@gmail.com> 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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Trouble using hashq-get-handle in Guile
2014-12-08 0:38 ` Mark H Weaver
@ 2014-12-08 0:49 ` jamil egdemir
2014-12-08 2:24 ` Mike Gran
0 siblings, 1 reply; 6+ messages in thread
From: jamil egdemir @ 2014-12-08 0:49 UTC (permalink / raw)
To: guile-user
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" #<hash-table 297f820 17/31> #<hash-table 2982700
17/31> #<hash-table 29896a0 17/31> #<hash-table 298f640 17/31> #\
<hash-table 29915e0 17/31> #<hash-table 299d580 17/31> #<hash-table
29a1520 17/31> #<hash-table 29a34c0 17/31> #<hash-table 29a6460 \
17/31> #<hash-table 29f7400 17/31> #<hash-table 29fd3a0 17/31>
#<hash-table 2a08340 17/31> #<hash-table 2a1c2e0 17/31> #<hash-table \
2a4b280 17/31> #<hash-table 2a96220 17/31> #<hash-table 2aa31c0 17/31>
#<hash-table 2aa5160 17/31> #<hash-table 2aaa100 17/31> #<has\
h-table 2ab10a0 17/31> #<hash-table 2abb040 17/31> #<hash-table
2accfe0 17/31> #<hash-table 2acef80 17/31> #<hash-table 2ad1f20 17/3\
1> #<hash-table 2af1ec0 17/31> #<hash-table 2af4e60 17/31>)
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
unclejamil@gmail.com
http://www.power-quant.com
(631) 338-3170 (cell)
-------------------------------------------------------------
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Trouble using hashq-get-handle in Guile
2014-12-08 0:49 ` jamil egdemir
@ 2014-12-08 2:24 ` Mike Gran
0 siblings, 0 replies; 6+ messages in thread
From: Mike Gran @ 2014-12-08 2:24 UTC (permalink / raw)
To: jamil egdemir, guile-user@gnu.org
On Sunday, December 7, 2014 4:49 PM, jamil egdemir <unclejamil@gmail.com> 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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-12-08 2:24 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-07 22:44 Trouble using hashq-get-handle in Guile jamil egdemir
2014-12-07 23:27 ` Thien-Thi Nguyen
2014-12-07 23:34 ` jamil egdemir
2014-12-08 0:38 ` Mark H Weaver
2014-12-08 0:49 ` jamil egdemir
2014-12-08 2:24 ` Mike Gran
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).