How to return array keys into another array

Avi Bryant avi at
Thu Jun 10 19:05:03 UTC 2004

On Jun 10, 2004, at 11:33 AM, Tim Rowledge wrote:

>> a := Array with: 'free' with: 'reserved' with: 'free' with: 
>> 'reserved'.
>> a2 := ((1 to: a size) inject: OrderedCollection new into: [ :oc :i | 
>> (a at:
>> i) = 'free' ifTrue: [ oc add: i ]. oc ]) asArray.
> How about
> a2 := a select:[:i| i = 'free]

No, because he wanted the indices, not the actual values.  However, I 
suspect this isn't the whole story; it's unusual enough to deal with 
array indices directly that I wonder what the final goal is here, and 
whether there might be some better way of achieving it...

Anyway, another option would be

a2 := Array streamContents: [:s | a withIndexDo: [:ea :i | ea = 'free' 
ifTrue: [s nextPut: i]]].


More information about the Squeak-dev mailing list