[Newbies] indexing into a collection

Louis LaBrunda Lou at Keystone-Software.com
Wed Jul 27 12:12:53 UTC 2016


Hi Joe & Ben,

Ben's example is a good one.  If there will be only one instance of DrivingDays in the
collection for a given date then you should look at Dictionary and LookupTable.  You could then
add items to the collection with:

collection at: aDrivingDays date put: aDrivingDays.

and then use:

item := collection at: aDate.

to fetch the instance you want.

Lou


On Wed, 27 Jul 2016 15:51:44 +0800, Ben Coman <btc at openinworld.com> wrote:

>On Wed, Jul 27, 2016 at 8:13 AM, Joseph Alotta <joseph.alotta at gmail.com> wrote:
>> Ben, Ron, I made a small example, and it works as you said it would:
>>
>> a := #( #(1 2)  #(3 4)).
>> b := a asOrderedCollection.
>> b  => an OrderedCollection(#(1 2) #(3 4))
>> c := b select: [ :i | (i first) = 3 ].
>
>Ahh. Code examples help. Maybe what you need is #detect:
>which returns an element rather than a collection of elements.
>
>> c => an OrderedCollection(#(3 4))
>> d := c first
>> d =>  #(3 4)
>> d at: 2 put: 5
>> d =>  #(3 5)
>> b  => an OrderedCollection(#(1 2) #(3 5))
>>
>> Okay, I will check my other code.
>>
>> Sincerely,
>>
>> Joe.
>
>Here's a domain specific example...
>
>Object subclass: #DrivingDays
>    instanceVariables: 'date store mileage"?" '
>    ...etc...
>
>oc := OrderedCollection new.
>oc add: (DrivingDays new date: '2016-01-02' ; store: 'quigley' ; mileage: 18).
>oc add: (DrivingDays new date: '2016-01-03' ; store: 'marianos' ; mileage: 5).
>
>oc printString.
>>> > an OrderedCollection(2016-01-02| quigley | 18,
>>> >                      2016-01-03| marianos | 5)
>
>drivingDayToUpdate := oc detect: [ :dd | dd store = 'marianos' ].
>drivingDayToUpdate mileage: 7.
>
>oc printString.
>>> > an OrderedCollection(2016-01-02| quigley | 18,
>>> >                      2016-01-03| marianos | 7)
>
>(disclaimer, I am not somewhere I can run this. Its just off the top
>of my head.)
>cheers -ben
>
>>
>>
>>
>>
>>> On Jul 26, 2016, at 6:25 PM, Ben Coman [via Smalltalk] <[hidden email]>
>>> wrote:
>>>
>>> Hi Joe,
>>>
>>> As Ron said, you should not be getting a copy.  If you still have a
>>> problem, perhaps best if you post code for a complete example:
>>> * class definition (with just two instance variables)
>>> * instance variable accessor methods
>>> * instance creation & adding to collection
>>> * select statement
>>> * updating object
>>>
>>> cheers -ben
>>>
>>> On Wed, Jul 27, 2016 at 5:53 AM, Ron Teitelbaum <[hidden email]> wrote:
>>>
>>> > Hi Joe,
>>> >
>>> > If the orderedCollection contains your object DrivingDays then select
>>> > should give you the object and not a copy.
>>> >
>>> > You don't need to add it back to the collection just update the object.
>>> >
>>> > Check your code for anything that might be making a copy.
>>> >
>>> > All the best,
>>> >
>>> > Ron Teitelbaum
>>> >
>>> >
>>> > -----Original Message-----
>>> > From: [hidden email] [mailto:[hidden email]] On Behalf Of Joseph Alotta
>>> > Sent: Tuesday, July 26, 2016 3:04 PM
>>> > To: [hidden email]
>>> > Subject: [Newbies] indexing into a collection
>>> >
>>> > Greetings,
>>> >
>>> > I have a OrderedCollection of DrivingDays.
>>> >
>>> >
>>> > an OrderedCollection(2016-01-02| quigley s| nil|18§
>>> >                      2016-01-03| marianos fresh| nil|5§
>>> >                      2016-01-04| fresh thyme| nil|5§
>>> >                      2016-01-05| panda express| nil|3§
>>> >                      2016-01-06| peets| nil|7§
>>> >                      2016-01-07| starbucks| nil|3§)
>>> >
>>> > I want to select aDrivingDay object from the list by date, update it by
>>> > adding mileage and places visited and put it back into the list.
>>> >
>>> > If I #select the OrderedCollection, I get a copy of the item, not the
>>> > same one in the OrderedCollection.
>>> >
>>> > How do I select an item in the list for update?
>>> >
>>> > Sincerely,
>>> >
>>> > Joe.
-- 
Louis LaBrunda
Keystone Software Corp.
SkypeMe callto://PhotonDemon



More information about the Beginners mailing list