我有一個看起來像這樣從XLSX文件中去掉數據:查找字符
[6, 2]=>"Comment", [6, 3]=>"Complaint", [6, 4]=>"Enquiry"
我想搜索的陣列和前括號返回第二個數字的值。即,搜索Complaint
應該返回3
。任何幫助表示讚賞。
NB - 根據對數組的註釋更新。刪除了OP中的字符串轉換。
require 'roo'
today_file=(File.dirname(__FILE__) + '/output/today-report.xlsx')
def by_value(data, value)
found = data.find do |k, v|
v == value
end
found and found[0][1]
end
data = Roo::Excelx.new (today_file)
output = by_value(data, "Complaint").inspect
puts output
當運行這個返回「零」
從這個陣列的輸出是這種形式:
{[1, 1]=>"YESTERDAY - 02/10/14", [1, 4]=>"Another comment below the fold - scroll down", [2, 1]=>"Yesterday we received 11 regarding the service.", [4, 1]=>"TYPE", [6, 1]=>"", [6, 2]=>"Comment", [6, 3]=>"Complaint", [6, 4]=>"Enquiry", [6, 5]=>"Total (Type)", [7, 1]=>"Editorial and other queries", [7, 2]=>1.0, [7, 3]=>7.0,...}
數據是哈希還是大格式字符串? – Linuxios 2014-11-04 16:14:26
從字面上看,你的數據結構如何?數組對象作爲鍵的散列?如果是這樣的話,那會很容易......如下所示:'data.key(string)[1]'如果data是該結構而'string'是您要查找的字符串。 – lurker 2014-11-04 16:14:49
如果它更容易,它可能非常容易。它目前是一個轉換爲字符串的散列。 – 2014-11-04 16:16:32