Tilman Baum
Tilman Baum

Reputation: 39

Read a file one line per tick

I model archaeological settlement distributions and dynamics. I want to annually update the number of houses at the settlement sites using a .csv file similar to this one:

    Settlement 1
    0
    1
    5
    ...

I have two questions on that:

1) I do not succeed with the procedure as proposed in the Netlogo manual. There it reads

    "to go
     if file-at-end? [ stop ]
      set data csv:from-row file-read-line
      tick
      end"

BUT csv:from-row results in an error message ("nothing named... is defined"). How do I do this?

2) I have more than just one site. Is there a way to use only one .csv - file such as

    Settlement 1,Settlement 2,Settlement 3
    0,0,0
    1,0,0
    5,0,1
    ...

and make sure that Settlement 1 updates houses according to column 1, Settlement 2 according to column 2 etc?

Thank you for your help! Til

Upvotes: 0

Views: 287

Answers (1)

Alan
Alan

Reputation: 9620

extensions [csv]
globals [settlements-list]  ;;to keep them in order
breed [settlements settlement]
settlements-own [n-houses]

to setup
  file-close-all
  ca
  create-settlements 3  ;;3 settlements, to match your example
  set settlements-list [self] of settlements ;;or sort if you wish
  file-open "c:/temp/temp.csv"
  let _trash file-read-line  ;;discard headers
end

to go
  file-open "c:/temp/temp.csv"
  if file-at-end? [ stop ]  ;;or at least, stop reading data!
  let _data csv:from-row file-read-line  ;;get data as list
  foreach n-values length settlements-list [?] [
    ask (item ? settlements-list) [set n-houses item ? _data]
  ]
  ask settlements [
    show n-houses
  ]
end

Upvotes: 3

Related Questions