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


-- 
<https://github.com/stomar/>