Gorillaz Fan
Gorillaz Fan

Reputation: 45

why am i unable to read strings form a txt file from net logo?

I am trying to read in net logo the following line from a txt file:

job1 1 1 15 25 90 3 1111 1100 0010 0110 1011 0 0 0 0 0 0 0 0 0 0 0

however, all the time i received: Expected a constant (line 1, character 5)

In this case i have many problems.

A) How can i make netlogo to read string "Job1" ?

B) Considering that the 10th number is a binary, how can i make to be headed as a string instead a number?

I appreciate your answer.

Gorillaz Fan

Upvotes: 2

Views: 367

Answers (1)

nldoc
nldoc

Reputation: 1209

I`m not quite sure if I really got, what you want to achieve. Do you want to read all elements of the "txt-file" as strings, but seperated by the white spaces? If yes, you could try to read the file character by character to check for the length of strings between the white spaces. Then go through the file again and extract these strings. Here is an example code of how it could be achieved. Perhaps there are more elegant versions, but this one works perfect for me:

globals
[
  char-list
  char-counter
  string-list
  current-char
]

to read

set char-counter 0
set char-list []
set string-list []
set current-char 0

;; Open the file and go through it, char by char to check where the blank spaces are
file-open "in.txt"

while [file-at-end? = false]
[
  ;; Save current char
  set current-char file-read-characters 1

  ;; Check if the char is a blank space...
  ifelse (current-char != " ")
    ;; If not, increase the length of the current string
    [
      set char-counter char-counter + 1
    ]
    ;; If yes, save the length of the previous string, and reset the char-counter
    [
      set char-list lput char-counter char-list
      set char-counter 0
    ]
]

file-close  

;; Now read the file again and extract only the strings which are not blank spaces
file-open "in.txt"

let i 0
while [i < length char-list]
[
  ;; Read the next number of characters as defined by the previously created char-list
  set string-list lput file-read-characters item i char-list string-list

  ;; Skip 1 space:
  set current-char file-read-characters 1

  ;; Increase i
  set i i + 1
]

file-close

end

Upvotes: 2

Related Questions