Hannah Lee
Hannah Lee

Reputation: 75

Converting list into agents in foreach loop

Here is a fraction of code that cause trouble.

to update-trustt
  let rank-tvmratio sort-on [tvmratio] turtles
  foreach rank-tvmratio tvmratio -> ask rank-tvmratio [set trustt (trustt + tvmratio)]
end

In this code, "rank-tvmratio" is supposed to bring list which looks like : [(turtle 56) (turtle 23) ... (turtle 9)]

What I want to do is to make each of those turtles to update "trustt" variable respectively, which is written in the 3rd line.

Following Bryan's advice I inserted "->" and specified "who" is doing "what".

Still, there's a new error message "Nothing name -> has been defined"

How can I solve this problem? I need your wise advice.

(Actually variable tvmratio is coming from program R, using RNetLogo package. Is that have something to do with this?)

Thank you

Upvotes: 0

Views: 273

Answers (1)

Bryan Head
Bryan Head

Reputation: 12580

I think this is probably want you're looking for:

to update-trustt
  let rank-tvmratio sort-on [tvmratio] turtles
  foreach rank-tvmratio [ t ->
    ask t [
      set trustt (trustt + rank-tvmratio)
    ]
  ]
end

There was a couple problems with your code. First, the command given to foreach needs to include an argument. You do this with the -> syntax. The t (for turtle) before the -> is the variable that each item in the list will be passed in as. Second, you need to explicitly ask t since foreach, unlike ask, doesn't have the agents run the command, it just runs the command with the items passed in one at a time as the argument to the command.

Upvotes: 1

Related Questions