为什么我无法从 net logo 的 txt 文件中读取字符串?
why am i unable to read strings form a txt file from net logo?
我正在尝试从 txt 文件中读取网络徽标中的以下行:
工作1 1 1 15 25 90 3 1111 1100 0010 0110 1011 0 0 0 0 0 0 0 0 0 0 0
然而,我一直收到:预期为常量(第 1 行,字符 5)
在这种情况下我有很多问题。
A) 如何使 netlogo 读取字符串 "Job1"?
B) 考虑到第 10 个数字是二进制数,我怎样才能将其作为字符串而不是数字作为标题?
感谢您的回答。
大猩猩粉丝
我不太确定我是否真的得到了你想要达到的目标。您想将 "txt-file" 的所有元素作为字符串读取,但由空格分隔吗?
如果是,您可以尝试逐字符读取文件以检查空格之间的字符串长度。然后再次浏览文件并提取这些字符串。这是如何实现它的示例代码。也许还有更优雅的版本,但这个版本非常适合我:
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
我正在尝试从 txt 文件中读取网络徽标中的以下行:
工作1 1 1 15 25 90 3 1111 1100 0010 0110 1011 0 0 0 0 0 0 0 0 0 0 0
然而,我一直收到:预期为常量(第 1 行,字符 5)
在这种情况下我有很多问题。
A) 如何使 netlogo 读取字符串 "Job1"?
B) 考虑到第 10 个数字是二进制数,我怎样才能将其作为字符串而不是数字作为标题?
感谢您的回答。
大猩猩粉丝
我不太确定我是否真的得到了你想要达到的目标。您想将 "txt-file" 的所有元素作为字符串读取,但由空格分隔吗? 如果是,您可以尝试逐字符读取文件以检查空格之间的字符串长度。然后再次浏览文件并提取这些字符串。这是如何实现它的示例代码。也许还有更优雅的版本,但这个版本非常适合我:
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