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.
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.
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:
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.
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
another array.
“numbers” in the string “amounts” which returns the sum of all amounts
numbers=[
probably contain integers and amounts[0…7] is a string.
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…