using inject to count elements in array
2014-06-25 15:29
453 查看
I'm trying to count the occurrences of elements in an array and save it in a hash. I'd like to use the inject function. I have this code:
I don't understand why I get the following error:
Also, I don't know how to fix it.
replaces memo with it. Your block returns integers. So, after first iteratation, your memo is not a hash anymore, it's an integer. And integers don't accept strings in their indexers.
Fix is easy, just return hash from the block.
You may prefer
a = ["the","the","a", "it", "it", "it"] a.inject(Hash.new(0)) {|hash,word| hash[word] += 1}
I don't understand why I get the following error:
TypeError: can't convert String into Integer from (irb):47:in `[]' from (irb):47:in `block in irb_binding' from (irb):47:in `each' from (irb):47:in `inject'
Also, I don't know how to fix it.
injectcalls your block with two parameters, memo and current element. It then takes return value of the block and
replaces memo with it. Your block returns integers. So, after first iteratation, your memo is not a hash anymore, it's an integer. And integers don't accept strings in their indexers.
Fix is easy, just return hash from the block.
a = ["the","the","a", "it", "it", "it"] a.inject(Hash.new(0)) {|hash,word| hash[word] += 1; hash }
You may prefer
each_with_objectbecause it doesn't replace memo. Note that
each_with_objectaccepts parameters in reverse (element first, memo second).
a.each_with_object(Hash.new(0)) {|word, hash| hash[word] += 1}
相关文章推荐
- Using POI to replace elements in WORD(.docx)(使用POI替换word中的特定字符/文字)
- Finding all elements(which are integer) in an array whose sum is equals to the element exists within
- How to Iterate over Array in Java 1.5 using foreach loop Example
- Using the five elements of fashion colors to create the best feng shui in auspicious Home
- Ruby: Count unique elements and their occurences in an array
- Using POI to replace elements in WORD(.docx/.doc)(使用POI替换word中的特定字符/文字)【改进】
- Count smaller elements on right side in an array.
- Matching Portions of A String to Elements of an Array in Ruby
- Count frequencies of all elements in array in O(1) extra space and O(n) time
- Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
- Ruby: Count unique elements and their occurences in an array
- #455 – Using ItemContainerStyle to Bind Data Elements in a Collection to a Grid
- Count occurrences of a number in a sorted array with duplicates using Binary Search
- GSR :the meaning of <load sharing elements> <refernce><refcount> in CEF and how to calculate them
- How to find size of array in C/C++ without using sizeof ?
- Using IPv6 to connect computer in dorm from our laboratory
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0
- An introduction to debugging in MSVC++ using Pseudoregisters(转自codeproject)
- Using the itemDoubleClick event to open nodes in a Flex Tree control
- A new way to inject backdoor in PHPWind