Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.6K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Simple Problem with "AssociateTo"

Posted 12 years ago
POSTED BY: pete p
2 Replies
Posted 12 years ago

thanks Peter - letry this out!

POSTED BY: pete p
Posted 12 years ago

Two problems:

First: AssociateTo needs the name of a variable (a1), not the variable contents (<||>). Similar issues can appear with AppendTo, see the first Possible Issues example on its documentation page. AssociateTo has HoldFirst, so it itself is not evaluating a1, but it appears such an evaluation is happening along the way while passing the argument into your extend function. One way of addressing that problem, where each line needs to get evaluated separately, i.e. in its own cell:

Clear[extend];
a1 = <||>;
extend[] := Module[{}, AssociateTo[a1, x -> RandomInteger[{1, 10}]]];
Do[extend[], {3}]

Second: You are associating the same key 'x' with a different value in each iteration, so all previous values get overwritten each time.You will either have to create different keys in each step, or try to Merge them (the latter, of course, could more easily be achieved by first creating the list of values, and creating an Association from it with the single key only in the last moment).

Hope this helps,

Peter

POSTED BY: Peter Fleck
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard