How to write values in files for each turtle?

余生长醉 提交于 2019-12-11 05:56:52

问题


How can I write values in files for each turtle ? For example, I have 100 turtles and I would like to write data specific to each turtle in 100 files. For the moment, my code writes data for all turtles in one file .txt:

to write-locations-to-file
 file-open "/home/reduan/IBM/outputs.txt"
 ask turtles [ 
  file-print (word who " ; " xcor " ; " ycor " ; " color " ; " [pcolor] of patch-here "\r\n" ) ]
end

Thanks in advance for your help.


回答1:


I'm not sure what it is exactly that you are having trouble with, but you can just open a different file for each turtle. In the example below, I used the who number to generate different file names, but you could use some other method, as long as all file names are unique.

to write-locations-to-files
 ask turtles [ 
   file-open (word "/home/reduan/IBM/outputs-" who ".txt")
   file-print (word who " ; " xcor " ; " ycor " ; " color " ; " [pcolor] of patch-here "\r\n" )
   file-close
 ]
end


来源:https://stackoverflow.com/questions/23856832/how-to-write-values-in-files-for-each-turtle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!