簡體   English   中英

遍歷兩個數組以在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

它應該返回以下內容:

{"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

請幫忙!

我建議將values數組切成keys數組的長度,然后將它們映射到一個hash數組中。 例如:

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

也許有一種更優雅的處理方式,但這應該可以幫助您解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM