List Info

Thread: Change the value of an item in a list




Change the value of an item in a list
country flaguser name
United States
2007-05-14 05:44:26

Dear netlogo-users,

I have a question about how I can change the value of an item in a list.
Especially I would like to increase the value of an item in a list
randomly by 1, e.g. list before: [0 1 2 0], list after [0 1 3 0] or [1
1 2 0]
I tried the replace-item command but couldn't make it work.

"set list replace-item (one-of list) [list] (item + 1)"

Does anybody could give an advice how to do it.

Thanks a lot.

__._,_.___
.

__,_._,___
Re: Change the value of an item in a list
country flaguser name
United States
2007-05-14 08:05:47

At 05:44 AM 5/14/2007, you wrote:
>I have a question about how I can change the value of an item in a list.
>Especially I would like to increase the value of an item in a list
>;randomly by 1, e.g. list before: [0 1 2 0], list after [0 1 3 0] or [1
>1 2 0]
>I tried the replace-item command but couldn't make it work.

Try something like this:

set ptr random (length my-list)
set this-one item ptr my-list
set my-list replace-item ptr (this-one + 1)

Teresa Carrigan
tcarr%40acm.org">tcarracm.org

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.467 / Virus Database: 269.7.0/803 - Release Date: 5/13/2007 12:17 PM

__._,_.___
.

__,_._,___
Re: Change the value of an item in a list
country flaguser name
United States
2007-05-14 09:01:21

Replace-item takes the item number (i.e. the index) of the item to be
replaced in the list, the list itself, and the *value* to replace that
item with. You are providing the *value* of the item... and--well,
I'm not sure what your placeholder "item" stands for.

Try this:

;; get a random index number
;; get and increment the item at that location
;; replace the item at that index with the incremented value

let index random (length the-list)
let new-value 1 + ( item index the-list )
set the-list replace-item index the-list new-value

Hope that helps!

~~James
http://www.turtlezero.com

On 5/14/07, ts273683 < ts273683%40yahoo.de">ts273683yahoo.de> wrote:
&gt; Dear netlogo-users,
>
> I have a question about how I can change the value of an item in a list.
&gt; Especially I would like to increase the value of an item in a list
>; randomly by 1, e.g. list before: [0 1 2 0], list after [0 1 3 0] or [1
> 1 2 0]
> I tried the replace-item command but couldn't make it work.
&gt;
> "set list replace-item (one-of list) [list] (item + 1)"
>
>; Does anybody could give an advice how to do it.
>
> Thanks a lot.

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )