1

我想构建一个嵌套的哈希数组。我有一个数组,branch看起来像这样:

branch = ["handbags", "womens-shoes", "womens-beauty", "womens-clothes"]

然后我有另一个数组,其中包含以下内容handbags

handbags = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]

我想在一个循环中将handbags数组插入到branch数组中,这样我就得到了这样的东西:

branch = {"handbags" => ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"], "womens-shoes"],...}

我这样尝试:

def insert_branch(branch, branch_index, candidate, candidate_index)
    branch[[branch_index], branch[branch_index]] ||= {}
    branch[[branch_index], branch[branch_index]] = candidate[candidate_index]  
end

在哪里

candidate[candidate_index] = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]

给我

无法将数组转换为整数

我怎样才能做到这一点?

4

6 回答 6

4

您不能使用与整数不同的对象来索引数组。您必须使用哈希。由于 branch 是一个数组,Ruby 期望一个 Integer 来索引每个元素,并且当您给它另一个数组时,它会尝试将其转换为 Integer,从而产生错误。尝试这个:

branch = Hash.new #unnecessary, but makes the class explicit
handbags = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]
branch["handbags"] = handbags

branch.inspect 将产生:

"{\"handbags\"=>[\"wallets\", \"backpacks\", \"clutches\", \"evening-handbags\", \"hobo-bags\", \"satchels\", \"shoulder-bags\", \"tote-bags\"]}"
于 2012-05-03T16:39:42.863 回答
1

如果你真的有:

array = ["foo", "bar"]
foo   = ...
bar   = ...

那么你就不走运了:你不能(以任何好的方式)根据其名称获取局部变量引用的对象。每当您发现自己想要按名称查找值时,您应该使用哈希来存储值以供以后检索。即你应该改变你的数据结构,而不是像这样:

array  = ["foo","bar"]
things = {
  "foo" => ...,
  "bar" => ...
}

...但是当然,您无需再做任何工作即可获得答案。

如果由于某种原因您不能将代码更改为使用单个 Hash 文字,但如果您可以更改代码以使用实例变量而不是局部变量,则可以这样做:

array  = ["foo", "bar"]
@foo   = ...
@bar   = ...
branch = Hash[ array.map{ |name| [name,instance_variable_get("@#{name}")] } ]

以上代码需要Ruby 1.9;如果您改用旧的 1.8,请这样说。

在行动中看到:

irb(main):001:0> a = %w[foo bar jim]
#=> ["foo", "bar", "jim"]

irb(main):002:0> @foo = %w[ 1 2 3 ]
#=> ["1", "2", "3"]
irb(main):003:0> @bar = %w[ 4 5 6 ]
#=> ["4", "5", "6"]
irb(main):004:0> @jim = %w[ 7 8 9 ]
#=> ["7", "8", "9"]

irb(main):006:0> Hash[ a.map{ |name| [name,instance_variable_get("@#{name}")] } ]
#=> {"foo"=>["1", "2", "3"], "bar"=>["4", "5", "6"], "jim"=>["7", "8", "9"]}
于 2012-05-03T17:11:26.667 回答
0

您尝试做的事情可以通过以下方式完成:

a = %w|Things Animals Cities|
things = %w|Desk Table Lamp|

a[0] = {a[0] => things}

a
=> [{"Things"=>["Desk", "Table", "Lamp"]}, "Animals", "Cities"]

在你的情况下,它是

branch[0] = {branch[0] => handbags}

编辑:使用元编程,您可以根据名称查找数组:

categories = %w|Things Animals Cities|
things = %w|Desk Table Lamp|
animals = %w|Dog Cat Turtle|
cities = %w|London Rome Madrid|

result = categories.map do |category| 
  { category => eval(category.downcase) }
end

result
=> [{"Things"=>["Desk", "Table", "Lamp"]},
    {"Animals"=>["Dog", "Cat", "Turtle"]},
    {"Cities"=>["London", "Rome", "Madrid"]}]

注意使用eval来获取Array按类别命名的值。

于 2012-05-03T16:55:22.487 回答
0

您的代码中存在索引问题。该声明:

branch[[branch_index], branch[branch_index]]

Ruby 将其理解为试图branch在索引处访问[branch_index](这不是有效的索引......它是一个数组)并查看branch[branch_index]许多元素(这也不是有效的元素数量......它是一个字符串)。

我认为你真正想要的是:

def insert_branch(branch, branch_index, candidate, candidate_index)
    branch[branch_index] = {branch[branch_index] => candidate[candidate_index]}
end
于 2012-05-03T16:41:37.310 回答
0

你所拥有的有点过。也许这会给你一些指导。

branch = ["handbags", "womens-shoes", "womens-beauty", "womens-clothes"]

handbags = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]

def insert_branch(catagories,catagories_index,branch)
  catagories[catagories_index] = Hash[catagories[catagories_index],branch] 
end

insert_branch(branch,0,handbags)


>> p branch  # OUTPUT IS
>> [{"handbags"=>["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]}, "womens-shoes", "womens-beauty", "womens-clothes"]
于 2012-05-03T16:48:20.743 回答
0

Ruby Array 类说:

数组是任何对象的有序整数索引集合。

所以问题是你的索引必须是一个整数,但你正在尝试使用字符串索引分配给数组。您可能会考虑将整个内容转换为哈希(以便您可以使用字符串作为键),或者您可以将哈希或子数组放入数组中正确索引处,如下所示:

idx = branch.index "handbags"
branch[idx] = { "handbags" => handbags }

您还应该查看 Array#zip,看看它是否会为您提供所需的功能。

于 2012-05-03T16:45:51.307 回答