From: Todd Benson Date: 2012-11-17T08:54:32+09:00 Subject: Re: Accumulate the value of an array if this is repeated On Fri, Nov 16, 2012 at 12:09 PM, wrote: > Am 16.11.2012 17:51, schrieb Joao Silva: > >> Hi All. >> >> I have an arrangement with a series of numbers and a string. What I'm >> trying to make is that when I read an array value compare it to the >> value of the string and accumulate certain portion of the string if this >> is repeated. >> >> I have the array "numbers": >> >> http://dpaste.com/832132/ >> >> and I have the string "amounts" >> >> http://dpaste.com/832133/ >> >> What I'm trying to make is that if I find a number of array "numbers" in >> the string "amounts", I return the "amount" contained in the string, but >> if the number of array "numbers" is repeated string "amounts" I returns >> the sum of the values contained in the string, then place it in >> another array. >> >> Here is the code: >> >> ###################################################################### >> >> http://dpaste.com/832134/ >> >> ######################################################################## >> >> for example if you found the number "20222999" contained in the array >> "numbers" in the string "amounts" which returns the sum of all amounts >> associated with that number, in this case would be: >> >> >> sum = 205.15 + 110.00 >> sum = 315.15 >> >> and value "sum" is assigned to a new array. >> >> Thanks. > > > Please shorten your examples and include the code in the E-Mail. > Also describe more explicitly what your specific problem/question is. > > As you already found out, your code can not work... > You will have to read up heavily on the basics and you should > also make it a habit to use irb for playing around with the different > parts of your code. > > E.g.: > > numbers=[ > 20222999 > 20777444 > 30222999 > ... > ] > > This is not even an array and gives a syntax error. > You need to separate array elements by comma. > > Or: > > numbers.include? ("amounts[0..7]") > > This searches for the exact string "amounts[0..7]", > which certainly can not be found in your data. > > You mean: > > numbers.include?(amounts[0..7]) > > which doesn't work either because the numbers array will > probably contain integers and amounts[0..7] is a string. > > 1.9.3p194 :001 > [12345, 23456].include?("12345") > => false > 1.9.3p194 :002 > [12345, 23456].include?(12345) > => true > > and so on... > > > One approach (there are many) to solve your problem would be: > > - split your amounts string into an array of single lines > - go through the array, split each entry into number and amount, > and build a hash with the numbers as keys and > an array with all the amounts for that number as value > - then go through your hash and add the values up Something like this? I'm making a hash from the second and running through the selected numbers against the dictionary (hash) after... numbers_str = "1 2 3 7 5" amounts_str = "1 22.4 3 46.2 9 2.0 1 15.15 2 14.0 5 2.4 1 1.5 3 17.25" # just to get them into array form converted numbers = numbers_str.split.map &.to_i amounts = amounts_str.split("\n").map {|e| e.split.map &:to_f} # go through amounts filling a hash h = Hash.new(0) amounts.each {|e| h[e[0].to_i] += e[1]} # print all sums p h # print sums for select numbers numbers.each {|n| p "#{n}: #{h[n]}"}