assign values to turtles by searching through CSV

2019-08-08 22:33发布

I have 3 producers. Each has 200 consumers as turtles who have purchased laptops. All producers sell laptops. The laptops have 3 kinds of features : size of the screen, price, battery life. Each has some levels. For example battery life has 5 hours, 12 hours, 24 hours, 30 hours. Each producer has some information about how consumers have rated these items based on what they have experienced that is stored in a csv file. So the CSV file header is something like this :

Consumer 12 13 13.5 14 14.5 15 500 600 750 650 550 700 5 12 24 30

( To exaplin the header : 12 - 15 represent levels of screen size, 500 - 700 price, 5 - 30 battery life in hours)

Each producer needs to analyze the data stored in the CSV file. I used the example code in the library to use a csv file which creates a turtle and assigns values to it using item.

to read-turtles-from-csv


file-close-all ; close all open files
if  not file-exists? "turtles.csv" [
    user-message "No file 'turtles.csv' exists! Try pressing WRITE-TURTLES-TO-CSV."
    stop
  ]

  file-open "turtles.csv" ; open the file with the turtle data

  ; We'll read all the data in a single loop
  while [ not file-at-end? ] [
    ; here the CSV extension grabs a single line and puts the read data in a list
    let data csv:from-row file-read-line
    ; now we can use that list to create a turtle with the saved properties
    create-turtles 1 [
      if    item 2 data >    item 2 data   ...
     if item 7 data < item 8 data ...

    ]
  ]

  file-close ; make sure to close the file
end

Here the csv file should not have a header row. The first row is selected for turtle 1. If we need to compare the consumer's rate for 2 levels of screen size like 13 and 13,5 , here it can do that :

If item 2 > item 3 ...

So far it works pretty well. Howeber, I need to use the header. For instance, I want to create a situation when a user chooses a number, if it is in one of the columns (is in the header), then the value gets selected. Otherwise, if the value does not exist in the header, somthing else should be done. Can I make the code recognize CSV header while working with agents?

Thanks,

0条回答
登录 后发表回答