我有这种格式的(游戏分数)数据:
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
射击之星俱乐部2430 5 339 1 2472 2
Gun Tooters 329 4 2512 2 2470 6
Banshee Wolves 301 1 2436 3 412 5
前两个/三个单词代表俱乐部的名字,此后每行有6个数据块,代表俱乐部的逐轮得分和对手指数(从1开始)。在上面的数据中,每个团队都进行了3轮比赛。 Hotspurs Giants (索引1)在第一轮得分356中打出 Banshee Wolves (6)至Banshee的301,在第2轮 Hotspurs Giants 中< em>射击之星俱乐部(4)得分275 - 339,第3轮打出 Mountain Tigers (3)得分442得到老虎的342
我的问题是如何以最有效的方式解析这些数据块,以便每个俱乐部的数据将采用以下格式,考虑到俱乐部的名称可能包含两(2)个或更多单词。
维兹
每个俱乐部[俱乐部回合分数对手得分]
答案 0 :(得分:3)
假设data
是:
data: [
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
]
我认为这解决了问题,请检查结果:
clubs: copy []
parse data [
some [
copy club some word!
copy numbers some number!
(append clubs reduce [form club numbers])
|
skip
]
]
new-line/all/skip clubs yes 2
list: copy []
parse clubs [
some [
set club string! into [
copy numbers some number! (
i: 1
foreach [score index] numbers [
append list reduce [
club score
pick clubs index * 2 - 1
pick pick clubs index * 2 i
]
i: i + 2
]
)
]
| skip
]
]
new-line/all/skip list yes 4
之后,如果你probe clubs
,你应该得到:
CLUBS is a block of value: [
"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]
]
如果您probe list
输出为:
LIST is a block of value: [
"Hotspurs Giants" 356 "Banshee Wolves" 301
"Hotspurs Giants" 275 "Shooting Stars Club" 339
"Hotspurs Giants" 442 "Mountain Tigers" 342
"Fierce Lions Club" 371 "Mountain Tigers" 2519
"Fierce Lions Club" 2520 "Gun Tooters" 2512
"Fierce Lions Club" 0 "Shooting Stars Club" 2472
"Mountain Tigers" 2519 "Fierce Lions Club" 371
"Mountain Tigers" 291 "Banshee Wolves" 2436
"Mountain Tigers" 342 "Hotspurs Giants" 442
"Shooting Stars Club" 2430 "Gun Tooters" 329
"Shooting Stars Club" 339 "Hotspurs Giants" 275
"Shooting Stars Club" 2472 "Fierce Lions Club" 0
"Gun Tooters" 329 "Shooting Stars Club" 2430
"Gun Tooters" 2512 "Fierce Lions Club" 2520
"Gun Tooters" 2470 "Banshee Wolves" 412
"Banshee Wolves" 301 "Hotspurs Giants" 356
"Banshee Wolves" 2436 "Mountain Tigers" 291
"Banshee Wolves" 412 "Gun Tooters" 2470
]
答案 1 :(得分:2)
以下是一个示例(使用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,一次读一行。
更新 - re:您的评论在Rebol 2 [在REBOL / Core 2.7.8.2.5(2011年1月2日)中测试]中也是相同的示例:
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]
]