我使用Ruby和有一個哈希值,稱之爲foo
,其值是具有2如何更新哈希值的數組?
一個固定長度的數組如何更新的散列值陣列中的索引中的一個?這裏有一個例子:
foo.each do |k, v|
if k == 'some value'
foo[k] = update v[0]
foo[k] = update v[1]
end
end
進一步澄清:
我循環通過一個文件,裏面我想看看如果當前行的哈希鍵k
相匹配。如果是這樣,我想更新存儲在v[1]
中的values數組中的時間戳。
# read lines from the input file
File.open(@regfile, 'r') do |file|
file.each_line do |line|
# cache control
cached = false
# loop through @cache
@cache.each do |k, v|
# if (url is cached)
if line == k
# update the timestamp
@cache[k] = Time.now.getutc # need this to be put in v[1]
# set cached to true
cached = true
end
end
# if cached go to next line
next if cached
# otherwise add to cache
updateCache(line)
end
end
爲什麼不只是設置foo的[K] [0] = NEW_VALUE; foo [k] [1] = new_value2 – klochner
顯示輸入/預期輸出的一些示例,以便我們可以更好地理解您的問題。 –
@klochner我打算試試,但我不確定是否打字foo [k] [1]會更新v的數組中的第二個位置。 – CoryDorning