Ruby将数组拆分为键:值对

hjzp0vay  于 2022-10-15  发布在  Ruby
关注(0)|答案(1)|浏览(142)

所以我有一个数组,其中key:value被记录为单个单位。我想拆分数组,并作为个体记录关键字和值。
示例

array = ["Image loaded:", "RuleName: xx", "UtcTime: xxx", "ProcessGuid: {xxx}", "ProcessId: 884", "Image: C:\\opt\\td-agent\\bin\\ruby.exe", "ImageLoaded: xxx", "FileVersion: -", "Description: -", "Product: -", "Company: -", "OriginalFileName: -", "Hashes: MD5=xxx", "Signed: false", "Signature: -", "SignatureStatus: Unavailable", "User: xx"]

现在,我想循环它并提取key:Value

LOOP THROUGH ARRAY
array[i] = array.split(',',key,value)
print(key)
print(value)
jmp7cifd

jmp7cifd1#

如果您希望构建一个拆分了键和值的新数组,您可以这样做:

new_array = array.map {|a| a.split(":")}
new_array[0][0] # First key value
new_array[0][1] # First value

相关问题