How do you import a CSV line-by-line in Netlogo?

Viewed 602

How do I import data from a CSV file and convert it line-by-line so I can use it in my program? I want it to be a string but I keep getting error messages that it is still a list or a list of lists.

I am trying to copy the example given in the Netlogo User Manual for reading a file one line per tick (see below). I created a simple example.

extensions [CSV]
globals [date]


to setup
  clear-all
  file-close-all
  file-open "S&PDate.csv"
  set date current-date
  reset-ticks
end

to go
   if file-at-end? [stop]
   set date current-date
   tick
end

to-report current-date 
   file-open "S&PDate.csv"
   let result csv:from-row file-read-line 
   while [ not file-at-end? ] [
   let row csv:from-row file-read-line
   set result (map result row)
     ]
   file-close
   report result
end

From NetLogo User Manual

1 Answers

My idea is to import the CSV data all at once as a list, then extract the value from the list by the number of ticks (i.e. line-by-line).

extensions [csv]
globals [data variable]

to setup
file-close-all 
file-open "S&PDate.csv"
;; read the data all at once by using csv:from-file
set data csv:from-file "S&PDate.csv"
reset-ticks
end

to go
if file-at-end? [stop]
;;extract value from the list, using item 0 to remove the list, and just keep the value
set variable item 0 item ticks data
tick  
if ticks = length data [stop]
show variable
end

Hope it helps. Please let me know if you need further clarification.

Related