Klabautermann
Klabautermann

Reputation: 31

How to read a text file in netlogo with as many characters as the world has patches and give the imported information to the patches?

patches-own[ a ]

to startup
  set a []
file-open "test.txt"
 while [ not file-at-end? ] [
 set a lput file-read a
  ]
  file-close
end 

this is how my code looks like (the important part for that problem). I have a text file with as many characters as patches are exist: [ 1 2 3 4 5 6 7 8 9 ] I now want to give the information from the text-file to the patches of my world. so the first patch (upper left end) should get the information 1. It should be really easy but it does not work. The code is just my test, probably there is a way nicer way to do it. I hope this is specific enough. Would be great if you guys can help me, thanks a lot.

Upvotes: 1

Views: 79

Answers (1)

Bryan Head
Bryan Head

Reputation: 12580

sort patches will create a list of patches from top left to bottom right (row-major order). Then, you can use foreach to iterate through the patches and file contents together:

(foreach (sort patches) a [ [p x] ->
  ask p [
    ; do stuff with x
  ]
])

Upvotes: 1

Related Questions