Getting a value from a specific field in a .CSV file


The code below can be used to retrieve a specific field in a .csv file. In my previous example, each row has 10 values(e.g. 1,1,1,1,4,2,2,2,1,3) and each value correspond to a specific field.


globals [csv s]
to clear
clear-all
file-close
set s []
end

to read
file-open "data-wm.csv"

if ( not file-at-end? )[
set csv file-read-line
set csv word csv ","
]

let mylist []

  while [not empty? csv]
   [ let $x position "," csv
     let $item substring csv 0 $x
     carefully [set $item read-from-string $item][]
     set mylist lput $item mylist
     set csv substring csv ($x + 1) length csv
   ]
 
ifelse length mylist != 0[
set s lput (item 1 mylist) s
][ stop]
end

to-report howMany [num frmlist]
  report reduce
    [ifelse-value (?2 = num) [?1 + 1] [?1]] (fput 0 frmlist)
end
Share on Google Plus

About SSabal

:)
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment