2

我有这种格式的(游戏分数)数据:

热刺巨人队 356 6 275 4 442 3

凶猛狮子会 371 3 2520 5 0 4

山虎队 2519 2 291 6 342 1

流星俱乐部 2430 5 339 1 2472 2

枪手 329 4 2512 2 2470 6

女妖狼 301 1 2436 3 412 5

前两个/三个单词代表俱乐部的名称,之后每行 6 个数据块,代表俱乐部的逐轮得分和对手指数(从 1 开始)。在上述数据中,每支球队已经进行了 3 轮比赛。热刺巨人队(指数 1)在第一轮对阵女妖狼队(6),得分 356,女妖队的 301,在第 2 轮热刺巨人队以275 - 339 的比分对阵流星俱乐部(4),在第 3 轮对阵山虎队(3)得分 442到老虎的 342

我的问题是如何以最有效的方式解析这些数据块,以便考虑到俱乐部名称可能包含两 (2) 个或更多单词,每个俱乐部的数据将采用以下格式。

[Club Round Score Opponent Opponent-Score] 每个俱乐部

4

2 回答 2

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
]
于 2013-08-14T11:56:43.780 回答
3

这是一个示例(使用 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]
]
于 2013-08-13T16:20:25.360 回答