如何访问哈希数组中的哈希值?

发布于 2024-10-21 10:56:19 字数 619 浏览 4 评论 0原文

我有一个标题为 entities 的哈希数组,

下面是该数组的外观:

  p "entities"
  y entities
  p "entities[0]"
  y entities[0]
  p "entities[1]"
  y entities[1]
  p "entities[2]"
  y entities[2]

输出:

"entities"
--- |
 --- 
 - :text: Berlin
   :links: 
   - http://dbpedia.org/resource/Berlin
 - :text: Jan Karski
   :links: 
   - http://dbpedia.org/resource/Jan_Karski
 - :text: God
   :links: 
   - http://dbpedia.org/resource/God
"entities[0]"
--- "-"
"entities[1]"
--- "-"
"entities[2]"
--- "-"

我不知道如何访问各个哈希值。当我执行 pEntity[0] 时,应该会出现什么情况吗?

I have an array of hashes titled entities

Here is a look at the array:

  p "entities"
  y entities
  p "entities[0]"
  y entities[0]
  p "entities[1]"
  y entities[1]
  p "entities[2]"
  y entities[2]

Output:

"entities"
--- |
 --- 
 - :text: Berlin
   :links: 
   - http://dbpedia.org/resource/Berlin
 - :text: Jan Karski
   :links: 
   - http://dbpedia.org/resource/Jan_Karski
 - :text: God
   :links: 
   - http://dbpedia.org/resource/God
"entities[0]"
--- "-"
"entities[1]"
--- "-"
"entities[2]"
--- "-"

I can't figure out how to access the individual hashes. Should something be coming up when I do p entities[0]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

|煩躁 2024-10-28 10:56:19

是entities.kind_of吗?字符串 正确吗?看起来实体实际上是一个 YAML 字符串。因此,entities[0/1/2] 是 YAML 的前三个破折号。

Is entities.kind_of? String true? It looks like entities is actually a YAML string. Hence, entities[0/1/2] are the first three dashes of the YAML.

灼痛 2024-10-28 10:56:19

抄对了吗?粘贴到 irb 会话中的输出作为单个字符串而不是数组出现:

ruby-1.9.2-p0 > YAML::parse("--- |
ruby-1.9.2-p0">  --- 
ruby-1.9.2-p0">  - :text: Berlin
ruby-1.9.2-p0">    :links: 
ruby-1.9.2-p0">    - http://dbpedia.org/resource/Berlin
ruby-1.9.2-p0">  - :text: Jan Karski
ruby-1.9.2-p0">    :links: 
ruby-1.9.2-p0">    - http://dbpedia.org/resource/Jan_Karski
ruby-1.9.2-p0">  - :text: God
ruby-1.9.2-p0">    :links: 
ruby-1.9.2-p0">    - http://dbpedia.org/resource/God
ruby-1.9.2-p0"> ").value
 => "--- \n- :text: Berlin\n  :links: \n  - http://dbpedia.org/resource/Berlin\n- :text: Jan Karski\n  :links: \n  - http://dbpedia.org/resource/Jan_Karski\n- :text: God\n  :links: \n  - http://dbpedia.org/resource/God\n" 

Did that copy right? the output, pasted into a irb session comes out as a single string, not an array:

ruby-1.9.2-p0 > YAML::parse("--- |
ruby-1.9.2-p0">  --- 
ruby-1.9.2-p0">  - :text: Berlin
ruby-1.9.2-p0">    :links: 
ruby-1.9.2-p0">    - http://dbpedia.org/resource/Berlin
ruby-1.9.2-p0">  - :text: Jan Karski
ruby-1.9.2-p0">    :links: 
ruby-1.9.2-p0">    - http://dbpedia.org/resource/Jan_Karski
ruby-1.9.2-p0">  - :text: God
ruby-1.9.2-p0">    :links: 
ruby-1.9.2-p0">    - http://dbpedia.org/resource/God
ruby-1.9.2-p0"> ").value
 => "--- \n- :text: Berlin\n  :links: \n  - http://dbpedia.org/resource/Berlin\n- :text: Jan Karski\n  :links: \n  - http://dbpedia.org/resource/Jan_Karski\n- :text: God\n  :links: \n  - http://dbpedia.org/resource/God\n" 
稀香 2024-10-28 10:56:19

您的entitles可能并不是真正的哈希数组。
看这个:

irb(main):001:0> e = [{:a=>1,:b=>2},{:c=>3,:d=>4}]
irb(main):002:0> p e
[{:a=>1, :b=>2}, {:c=>3, :d=>4}]

尝试 entitles.classentitles[0].class

Your entitles may be not really array of hashes.
Look this:

irb(main):001:0> e = [{:a=>1,:b=>2},{:c=>3,:d=>4}]
irb(main):002:0> p e
[{:a=>1, :b=>2}, {:c=>3, :d=>4}]

Try entitles.class and entitles[0].class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文