really silly question

Doug Way dway at riskmetrics.com
Fri Aug 31 03:21:27 UTC 2001


On Thursday, August 30, 2001, at 10:49 PM, Jon Hylands wrote:

> On Fri, 31 Aug 2001 03:36:19 +0100, John Hinsley 
> <jhinsley at telinco.co.uk>
> wrote:
>
>> Given two arrays of the same size filled with numbers, can someone give
>> me a code snippet that will show me how to add the numbers at the same
>> index to produce a third array?
>>
>> So that firstArray:= #(1 2 3 4)
>> 	secondArray:= #(5 6 1 2)
>>
>> will result in a thirdArray (6 8 4 6)
>
> Try:
>
> thirdArray := (1 to: firstArray size) collect: [:index |
> 	(firstArray at: index) + (secondArray at: index)]

This might be a touch clunkier than Jon's, but it's always good to see 
more than one example:

coll := OrderedCollection new.
firstArray with: secondArray do: [:el1 :el2 | coll add: el1 + el2].
thirdArray := coll asArray.

- Doug Way
   dway at riskmetrics.com




More information about the Squeak-dev mailing list