这是一个示例(使用 Rebol 3),展示了如何做到这一点:
club-data: map [] ; store data in hash map is one option
foreach line read/lines %games-scores.txt [
fields: split line space
; lets take last 6 cols of data
scores: reverse collect [loop 6 [keep to-integer take/last fields]]
; and whats left is the club name
club-name: form fields
; build club data
club-data/(club-name): scores
]
以上假设数据在文件中games-scores.txt
并返回给您一个 MAP!(哈希图)调用club-data
您的俱乐部数据如下所示:
make map! [
"Hotspurs Giants" [356 6 275 4 442 3]
"Fierce Lions Club" [371 3 2520 5 0 4]
"Mountain Tigers" [2519 2 291 6 342 1]
"Shooting Stars Club" [2430 5 339 1 2472 2]
"Gun Tooters" [329 4 2512 2 2470 6]
"Banshee Wolves" [301 1 2436 3 412 5]
]
一个警告... READ/LINES 会将整个文件加载到内存中。因此,如果games-scores.txt
很大,您应该考虑改用 OPEN 并一次读取一行。
更新- 回复:您的评论与 Rebol 2 中的相同示例 [在 REBOL/Core 2.7.8.2.5 (2-Jan-2011) 中测试]:
club-data: make hash! [] ; of course doesn't have to be hash!
foreach line read/lines %games-scores.txt [
fields: parse line none
scores: reverse collect [loop 6 [keep to-integer take/last fields]]
club-name: form fields
append club-data reduce [club-name scores]
]