遍历两个数组以在 Ruby 中生成哈希
Iterating through two arrays to make a hash in Ruby
我有两个数组需要组合成一个散列。但是,其中一个数组将始终是关键。所以我需要浏览一个包含姓名、号码、地址等的列表,并给他们所有的头衔。一个例子是:
Adjustor name => Chase, Firm name => Chase Bank
然后重复另一个位置。
Adjustor name => Rob, Firm name => Walmart.
这是我目前的情况:
Array_headers = ['Adjuster Name','Firm Name', 'Firm Number', 'Adjustment Type', 'Address 1', 'Address 2', 'City', 'State', 'Zip Code', 'Phone', 'Fax', 'Website', 'Comments', 'Latitude', 'Longitude', 'Manual LongLatCalc', 'LongLat Error']
Data_examples = ["AdjusterName", "FirmName", "FirmNumber", "AdjustmentType", "Address1", "Address2", "City", "State", "ZipCode", "Phone", "Fax", "WebSite", "Comments", "Latitude", "Longitude", "ManualLongLatCalc", "LongLatError", "chase", "chase bank", "260-239-1761", "property", "501 w", "200 s", "albion", "in", "46701", "555-555-5555", "c@gamil", "whatsupwhatups.com", "hahahah", "12.332", "12.222", "no", "none"]
CombiningArrays= Hash[Array_headers.zip data_examples]
p CombiningArrays
应该return如下:
{"Adjuster Name"=>"AdjusterName", "Firm Name"=>"FirmName", "Firm Number"=>"FirmNumber", "Adjustment Type"=>"AdjustmentType", "Address 1"=>"Address1", "Address 2"=>"Address2", "City"=>"City", "State"=>"State", "Zip Code"=>"ZipCode", "Phone"=>"Phone", "Fax"=>"Fax", "Website"=>"WebSite", "Comments"=>"Comments", "Latitude"=>"Latitude", "Longitude"=>"Longitude", "Manual LongLatCalc"=>"ManualLongLatCalc", "LongLat Error"=>"LongLatError", *"Adjuster Name"=>" \r\nchase", "Firm Name"=>"chase", "Firm Number"=>"260-239-1761", "Adjustment Type"=>"property", "Address 1"=>"501 w", "Address 2"=>"200 s", "City"=>"albion", "State"=>"in", "Zip Code"=>"46701", "Phone"=>"555-555-5555", "Fax"=>"c@gamil", "Website"=>"whatsupwhatups.com", "Comments"=>"hahahah", "Latitude"=>"12.332", "Longitude"=>"12.222", "Manual LongLatCalc"=>"no", "LongLat Error"=>"none"*}
它停在 "LongLat Error"=>"LongLatError"
并且所有斜体字都不会显示。我如何让它不断循环我的其他阵列?
我也试过下面的代码:
#创建一个遍历数组的方法
def array_hash_converter headers, data
hash = Hash.new
headers.each_with_index do |header, index|
hash[header] = data[index]
end
puts hash
end
i=0
while i < data.count do
array_hash_converter Array_header, data
i+=1
end
请帮忙!
我建议根据键数组长度对值数组进行切片,然后将它们映射到哈希数组中。例如:
sliced_values = Data_examples.each_slice(Array_headers.length)
result = sliced_values.map { |slice| Array_headers.zip(slice).to_h }
你永远不会得到一个单一的散列结果,因为你会在键上发生冲突,然后,只有最后一个结果会被返回,因为它会覆盖以前的结果。请记住,散列键在 Ruby.
中是唯一的
看起来您确实需要一个哈希数组。因此,您的第一个数组将是您的哈希键列表(我将其称为 "HEADER_KEYS")。在您的第二个数组中,我看到“\r\n”。您可能想要备份一个步骤。我假设这是来自 CSV,所以有已知的分隔符和未知的行数。要开始解析您的 CSV,请在“\r\n”(或换行符恰好是什么)处拆分,然后遍历每个项目并在逗号处拆分。就像是:
final_dataset = []
HEADER_KEYS = [].freeze # put your actual array_of_headers here and freeze it
array_of_rows = []
csv_string.split("\r\n").each { |row| array_of_rows.push(row.split) }
这应该会为您提供一个可以循环的数组数组。
array_of_rows.each do |row|
row_hash = {}
HEADER_KEYS.each_with_index do |key, index|
row_hash[key] = row[index]
end
final_dataset.push(row_hash)
end
可能有更优雅的方法来处理这个问题,但这应该可以让你继续下去。
我有两个数组需要组合成一个散列。但是,其中一个数组将始终是关键。所以我需要浏览一个包含姓名、号码、地址等的列表,并给他们所有的头衔。一个例子是:
Adjustor name => Chase, Firm name => Chase Bank
然后重复另一个位置。
Adjustor name => Rob, Firm name => Walmart.
这是我目前的情况:
Array_headers = ['Adjuster Name','Firm Name', 'Firm Number', 'Adjustment Type', 'Address 1', 'Address 2', 'City', 'State', 'Zip Code', 'Phone', 'Fax', 'Website', 'Comments', 'Latitude', 'Longitude', 'Manual LongLatCalc', 'LongLat Error']
Data_examples = ["AdjusterName", "FirmName", "FirmNumber", "AdjustmentType", "Address1", "Address2", "City", "State", "ZipCode", "Phone", "Fax", "WebSite", "Comments", "Latitude", "Longitude", "ManualLongLatCalc", "LongLatError", "chase", "chase bank", "260-239-1761", "property", "501 w", "200 s", "albion", "in", "46701", "555-555-5555", "c@gamil", "whatsupwhatups.com", "hahahah", "12.332", "12.222", "no", "none"]
CombiningArrays= Hash[Array_headers.zip data_examples]
p CombiningArrays
应该return如下:
{"Adjuster Name"=>"AdjusterName", "Firm Name"=>"FirmName", "Firm Number"=>"FirmNumber", "Adjustment Type"=>"AdjustmentType", "Address 1"=>"Address1", "Address 2"=>"Address2", "City"=>"City", "State"=>"State", "Zip Code"=>"ZipCode", "Phone"=>"Phone", "Fax"=>"Fax", "Website"=>"WebSite", "Comments"=>"Comments", "Latitude"=>"Latitude", "Longitude"=>"Longitude", "Manual LongLatCalc"=>"ManualLongLatCalc", "LongLat Error"=>"LongLatError", *"Adjuster Name"=>" \r\nchase", "Firm Name"=>"chase", "Firm Number"=>"260-239-1761", "Adjustment Type"=>"property", "Address 1"=>"501 w", "Address 2"=>"200 s", "City"=>"albion", "State"=>"in", "Zip Code"=>"46701", "Phone"=>"555-555-5555", "Fax"=>"c@gamil", "Website"=>"whatsupwhatups.com", "Comments"=>"hahahah", "Latitude"=>"12.332", "Longitude"=>"12.222", "Manual LongLatCalc"=>"no", "LongLat Error"=>"none"*}
它停在 "LongLat Error"=>"LongLatError"
并且所有斜体字都不会显示。我如何让它不断循环我的其他阵列?
我也试过下面的代码:
#创建一个遍历数组的方法
def array_hash_converter headers, data
hash = Hash.new
headers.each_with_index do |header, index|
hash[header] = data[index]
end
puts hash
end
i=0
while i < data.count do
array_hash_converter Array_header, data
i+=1
end
请帮忙!
我建议根据键数组长度对值数组进行切片,然后将它们映射到哈希数组中。例如:
sliced_values = Data_examples.each_slice(Array_headers.length)
result = sliced_values.map { |slice| Array_headers.zip(slice).to_h }
你永远不会得到一个单一的散列结果,因为你会在键上发生冲突,然后,只有最后一个结果会被返回,因为它会覆盖以前的结果。请记住,散列键在 Ruby.
中是唯一的看起来您确实需要一个哈希数组。因此,您的第一个数组将是您的哈希键列表(我将其称为 "HEADER_KEYS")。在您的第二个数组中,我看到“\r\n”。您可能想要备份一个步骤。我假设这是来自 CSV,所以有已知的分隔符和未知的行数。要开始解析您的 CSV,请在“\r\n”(或换行符恰好是什么)处拆分,然后遍历每个项目并在逗号处拆分。就像是:
final_dataset = []
HEADER_KEYS = [].freeze # put your actual array_of_headers here and freeze it
array_of_rows = []
csv_string.split("\r\n").each { |row| array_of_rows.push(row.split) }
这应该会为您提供一个可以循环的数组数组。
array_of_rows.each do |row|
row_hash = {}
HEADER_KEYS.each_with_index do |key, index|
row_hash[key] = row[index]
end
final_dataset.push(row_hash)
end
可能有更优雅的方法来处理这个问题,但这应该可以让你继续下去。