Problem with the CSV extension

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with the CSV extension

f.heidtmann
Dear all

I am a new user of NetLogo and would like to pose a question to one of the
functions of the csv-extension.

My code seems like the following:
--------------------
extensions [ csv ]

globals [ x y z ]

to setup
  ca
  ... ;; this code isnĀ“t relevant for this topic
  reset-ticks
end

to go
  problem
  tick
end

to problem
  file-open "test.csv"
  set x csv:from-row file-read-line
  set y random 100
  set z x - y
  ifelse z < 10 [
    set z 5 ] [
    set z 20 ]
end
----------------------
If I start the go-procedure, I will get the Runtime-Error "- expected input
to be a number but got the list [20] instead".
Does anyone have an idea how the problem can be solved?

I am also interested in contact to other modelers. I have started to build a
model referring to the energysystem analyses and would welcome the
opportunity to exchange experiences.

I look forward to receiving your responses.

Kind regards

Florian



--
Sent from: http://netlogo-users..18673.x6.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Problem with the CSV extension

cstaelin
It looks like your problem is that `x` is read in as a list, but you then use it as a single number in your `set z x - y` statement.  `csv:read:from-row` always produces a list, even if the row contains only a single number, so if the line in the file contained something like a single "2", the `csv:from-row` command would yield a list with a single element, [2].  To get at that element, you might use `set x first x`.

Reply | Threaded
Open this post in threaded view
|

Re: Problem with the CSV extension

Bryan Head
In reply to this post by f.heidtmann
Hi Florian,

From your code, it looks like your CSV file is just one number per row,
yes? Since CSV normally has a list of values on each row `csv:from-row`
returns a list. If you only want the first (and only) value in it, just do:

set x first csv:from-row file-read-line

Hope that helps!
Bryan