Dictionary and Association

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Dictionary and Association

Wolfgang Dann
Hi,

I don't understand what's wrong with this one:

st> dict := Dictionary new
Dictionary (
)
st> assoc := Association key: #key1 value: 'value1'
#key1->'value1'
st> dict add: assoc
#key1->'value1'
st> dict
Dictionary (
        #key1->'value1'
)
st> assoc key: #key2 value: 'value2'
#key2->'value2'
st> dict
Dictionary (
        #key2->'value2'
)
st> dict add: assoc
#key2->'value2'
st> dict
Dictionary (
        #key2->'value2'
        #key2->'value2'
)

best regards



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary and Association

MrGwen
Hi,

you can simply do:

dict := Dictionary new.
dict at: #key1 put: 'value1'.
dict at: #key2 put: 'value2'.

In your case the problem is the assoc variable (you just change the
content) here is the solution:

dict := Dictionary new.
assoc := Association key: #key1 value: 'value1'. " or #key1->'value1' "
dict add: assoc.
assoc := Association key: #key2 value: 'value2'. " or #key2->'value2' "
dict add: assoc.

Gwen

On 04/06/2011 02:43 PM, Wolfgang Dann wrote:

> Hi,
>
> I don't understand what's wrong with this one:
>
> st>  dict := Dictionary new
> Dictionary (
> )
> st>  assoc := Association key: #key1 value: 'value1'
> #key1->'value1'
> st>  dict add: assoc
> #key1->'value1'
> st>  dict
> Dictionary (
> #key1->'value1'
> )
> st>  assoc key: #key2 value: 'value2'
> #key2->'value2'
> st>  dict
> Dictionary (
> #key2->'value2'
> )
> st>  dict add: assoc
> #key2->'value2'
> st>  dict
> Dictionary (
> #key2->'value2'
> #key2->'value2'
> )
>
> best regards
>
>
>
> _______________________________________________
> help-smalltalk mailing list
> [hidden email]
> http://lists.gnu.org/mailman/listinfo/help-smalltalk


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary and Association

Wolfgang Dann
Am Mittwoch, den 06.04.2011, 15:07 +0200 schrieb Gwenael Casaccio:

> Hi,
>
> you can simply do:
>
> dict := Dictionary new.
> dict at: #key1 put: 'value1'.
> dict at: #key2 put: 'value2'.
>
> In your case the problem is the assoc variable (you just change the
> content) here is the solution:
>
> dict := Dictionary new.
> assoc := Association key: #key1 value: 'value1'. " or #key1->'value1' "
> dict add: assoc.
> assoc := Association key: #key2 value: 'value2'. " or #key2->'value2' "
> dict add: assoc.
>

I wanted to subclass Association to store a Bookmark.
If i understand you right, then i have to
always send new when the key changes.

thank you in advance


> >
> > _______________________________________________
> > help-smalltalk mailing list
> > [hidden email]
> > http://lists.gnu.org/mailman/listinfo/help-smalltalk
>
>
> _______________________________________________
> help-smalltalk mailing list
> [hidden email]
> http://lists.gnu.org/mailman/listinfo/help-smalltalk



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary and Association

Paolo Bonzini-2
On 04/06/2011 03:25 PM, Wolfgang Dann wrote:
> I wanted to subclass Association to store a Bookmark.
> If i understand you right, then i have to
> always send new when the key changes.

Sets and Dictionaries are not able to store objects or keys
(respectively for Sets and Dictionary) that change.  When you have a
change you need to take out the object (association for Dictionary),
change it and put it back.

Usually Associations are considered an implementation detail and you put
the Bookmark as the value.  You can then define a BookmarkList that
includes a Dictionary or LookupTable and automatically manages the
key->bookmark association so that the collection stays consistent.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk