I have to collect x-y coordinates of nodes of a network, from a csv file which has lines like this [X,Y]. The objective is to assign all the x coordinates and all the y coordinates to two related arrays which have tha range corresponding to the nodes number. I saw the driver csv usage but I did not get how to "read" every line of the csv file.

Thank you in advance for any help.

I guess that your question is related to Mosel. Note that the file format that you described is not really a CSV file. For that reason, it is best to read each line manually. Lets assume that your input file has the following structure:

[1.2, 2.3]

[-3.4, +4.5]

[5.6, -6.7]

Then you can use, for example, the following code to read the X and Y values into two arrays:

model "read coordinates from TXT"

uses "mmsystem"

declarations

I: range

X, Y: array(I) of real

t: text

ct: integer

end-declarations

fopen("filename.txt", F_INPUT)

while (readtextline(t) > 0) do

trim(t) ! Remove trailing blanks

if startswith(t,"[") then

ct += 1

X(ct) := parsereal(t, 2)

Y(ct) := parsereal(t, getparam("sys_endparse")+1)

end-if

end-do

fclose(F_INPUT)

forall (i in I) writeln(i,': [', X(i), ", ", Y(i), ']')

end-model

If you input file is a real CSV file that has the following structure,

1, 1.2, 2.3

2, -3.4, +4.5

3, 5.6, -6.7

where the first column is the index ID, the second is the x value and the third is the y value, then you can directly use the CSV driver as follows:

model "read coordinates from CSV"

declarations

I: range

X, Y: array(I) of real

end-declarations

initializations from "mmsheet.csv:filename.csv"

[X, Y] as "[A:C]"

end-initializations

forall (i in I) writeln(i,': [', X(i), ", ", Y(i), ']')

end-model

Regards,

Johannes