0

ハッシュテーブルのキーに関連付けられた値を取得するのに問題があり、何が間違っているのか完全に混乱しています。私が使用しているコードを以下に示します。

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

これは、ウェブサーバーと通信し、応答を解析するために、guile-curlguile-jsonに依存しています。私が走るまで、物事はかなり簡単に思えます。サーバーからの応答を json で見ることができます (非常に長く、ここには掲載されていません)。

scheme@(guile-user)> (hash-table? queryResults)
$25 = #t

したがって、queryResults が実際にハッシュ テーブルであることはわかっています。print-hash を使用してキーと関連する値を見ると、次のことがわかります。

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>)

それは良いことであり、それは私が期待したものです。しかし、hashq-get-handle を使用して totalNumRecords またはドキュメントの値を確認しようとすると、次のようになります。

scheme@(guile-user)> (hashq-get-handle queryResults 'totalNumRecords)
$24 = #f

scheme@(guile-user)> (hashq-get-handle queryResults 'documents)
$24 = #f

これは、ハッシュ テーブル queryResults にキーが存在しないことを示しているようです。私は一晩中、引用符を付けて、引用符を付けずに、SRFI-69 ハッシュテーブルの実装をいじってみました (その間に、ネイティブの guile ハッシュテーブルと SRFI-69 ハッシュテーブルの違いを発見しました)。Guile リファレンス マニュアルのハッシュ テーブルの例(すべて正常に動作しました)。私はアイデアがありません。

hashq-get-handle への呼び出しが期待どおりに機能しない理由を誰かが理解するのを手伝ってくれたら、とても感謝しています。

更新: guile-user メーリング リストからの ttn の提案に従います。

scheme@(guile-user)> (hash-get-handle queryResults 'totalNumRecords)
$27 = #f
scheme@(guile-user)> (hashv-get-handle queryResults 'totalNumRecords)
$28 = #f

だから、結果は同じようです。愛がない。

4

1 に答える 1

0

guile-user メーリング リストでのこの議論 (Thien-Thi Nguyen と Mark H Weaver の両方による) から、ここに要約された解決策が得られました。

json コードをざっと見てみると、次のものが必要であると思われます。

(hash-get-handle queryResults "ドキュメント")

つまり、'hashq-get-handle' を 'hash-get-handle' に変更し、記号ではなく文字列をキーとして渡します。

最終的には次のようになります。

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>)

そして、マークが行ったさらなる提案は、理にかなっています。

「print-hash」で「~a」の代わりに「~s」を使用すると、文字列が二重引用符内に出力されるため、キーが文字列か記号かが明確になります。

于 2014-12-08T01:22:42.800 に答える