![](/img/trans.png)
[英]Iterating over array of Ruby Hashes and counting the no. of times value has appeared?
[英]Explanation of ruby code for iterating over an Array of hashes
我有兩個腳本,它們的行為各不相同,一次運行兩次,另兩次運行兩次,無法理解為什么任何一個plz都能幫助您?
腳本-1
@newArray=[{grade_id:"1",sections:"4"},
{grade_id:"2",sections:"8"},
{grade_id:"3",sections:"7"},
{grade_id:"4",sections:"7"},
{grade_id:"5",sections:"3"},
{grade_id:"6",sections:"3"},
{grade_id:"7",sections:"3"}]
@total_grades_passed = @newArray.count
@grades_counter = 0
while @grades_counter < @total_grades_passed do
@newArray[@grades_counter].each do |key, value|
print [key]
puts [value]
end
@grades_counter += 1
end
給出以下輸出。
[:grade_id]1
[:sections]4
[:grade_id]2
[:sections]8
[:grade_id]3
[:sections]7
[:grade_id]4
[:sections]7
[:grade_id]5
[:sections]3
[:grade_id]6
[:sections]3
[:grade_id]7
[:sections]3
而SCRIPT-2則提供完全不同的輸出。
@newArray=[{grade_id:"1",sections:"4"},
{grade_id:"2",sections:"8"},
{grade_id:"3",sections:"7"},
{grade_id:"4",sections:"7"},
{grade_id:"5",sections:"3"},
{grade_id:"6",sections:"3"},
{grade_id:"7",sections:"3"}]
@total_grades_passed = @newArray.count
@grades_counter = 0
while @grades_counter < @total_grades_passed do
@newArray[@grades_counter].each do |x,y|
@grade_id = @newArray[@grades_counter][:grade_id].to_i
@no_of_sections = @newArray[@grades_counter][:sections].to_i
puts "Grade id is #{@grade_id} and its class is #{@grade_id.class}"
puts "section is #{@no_of_sections} and its class is #{@no_of_sections.class}"
end
@grades_counter += 1
end
提供以下奇怪的輸出。
Grade id is 1 and its class is Fixnum
section is 4 and its class is Fixnum
Grade id is 1 and its class is Fixnum
section is 4 and its class is Fixnum
Grade id is 2 and its class is Fixnum
section is 8 and its class is Fixnum
Grade id is 2 and its class is Fixnum
section is 8 and its class is Fixnum
Grade id is 3 and its class is Fixnum
section is 7 and its class is Fixnum
Grade id is 3 and its class is Fixnum
section is 7 and its class is Fixnum
Grade id is 4 and its class is Fixnum
section is 7 and its class is Fixnum
Grade id is 4 and its class is Fixnum
section is 7 and its class is Fixnum
Grade id is 5 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 5 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 6 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 6 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 7 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 7 and its class is Fixnum
section is 3 and its class is Fixnum
誰能解釋一下為什么會這樣,兩個腳本及其后續輸出之間的區別是由於哪幾行代碼引起的? 我只想提取grade_id和section_id並進行一些計算,然后移至下一對成績和科目ID,並對它們進行相同的計算。
有更好,更清潔,更“紅褐色”的方式來執行此操作。
如果grade_id和sections是您想要的,則可以這樣訪問:
@newArray.each do |hash|
hash.each do |grade_id, sections|
# do something
end
end
還請注意,當您遍歷哈希時,哈希將傳遞與密鑰相同的次數。 這就是為什么您的文本輸出兩次的原因。
@newArray[@grades_counter].each
內部的塊代碼將運行2次,因為每個哈希都有2對鍵值。
以及較短的代碼版本:
@newArray.each do |hash|
hash.each do |key, value|
print [key]
puts [value]
end
end
在第二個示例中,您不想遍歷哈希。 您正在遍歷哈希鍵(即使您不使用它)。
您應該這樣做:
@newArray=[{grade_id:"1",sections:"4"},{grade_id:"2",sections:"8"},
{grade_id:"3",sections:"7"},{grade_id:"4",sections:"7"},
{grade_id:"5",sections:"3"},{grade_id:"6",sections:"3"},
{grade_id:"7",sections:"3"}]
@total_grades_passed = @newArray.count
@grades_counter = 0
while @grades_counter < @total_grades_passed do
@grade_id = @newArray[@grades_counter][:grade_id].to_i
@no_of_sections = @newArray[@grades_counter][:sections].to_i
puts "Grade id is #{@grade_id} and its class is #{@grade_id.class}"
puts "section is #{@no_of_sections} and its class is #{@no_of_sections.class}"
@grades_counter += 1
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.