user710
user710

Reputation: 161

updating the value of items in a list in netlogo

I have 4 producers who have different attributes such as their new product's price, size, customer rates. I defined 4 lists representing them.

set att-price ((list p1-pr p2-pr p3-pr p4-pr)) ;prices of all the products of 4 producers

set att-size ((list p1-sz p2-sz p3-sz p4-sz))



set att-rates ((list p1-rt p2-rt p3-rt p4-rt))

As the time passes, the prices get updates, so I defined this to make this happen :

set (item 0 att-price) (item 0 att-price) * 0.20 ; changes in the price of product of producer one

set (item 1 att-price) (item 1 att-price) * 0.08

set (item 3 att-price) (item 3 att-price) * 0.43

But it has an error saying that "This isn't what you can "set" on"!

How can I update those items then? Thanks

Upvotes: 1

Views: 910

Answers (1)

Bryan Head
Bryan Head

Reputation: 12580

You use replace-item for this. For instance:

set att-price replace-item 0 att-price (0.2 * item 0 att-price) 

That is, rather than setting the items of the list, we're making a new list with the item replaced, and then setting our list variable to that item.

If you want to replace all items at once, you can use map. For instance, it looks like you might have a list of price ratios by which your prices change:

let ratios [ 0.2 1.0 0.08 0.43 ]
set att-price (map [ [ price ratio ] -> price * ratio ] att-price ratios)

Upvotes: 3

Related Questions