タブ区切りデータの処理 どう書く?org の問題を解いてみた。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| def text = new File("test.csv").text
def lines = text.split("n")
def titles = lines[0].split("t")
def records = lines[1..-1].collect {
it.split("t")
}
// 第1カラムの値でデータを昇順にソートする。
records.sort { it[0] }
// 第2カラムと第3カラムをヘッダを含めて入れ替える。
def change(row) {
def tmp = row[2]
row[2] = row[1]
row[1] = tmp
}
change(titles)
records.each {
change(it)
}
// 第4カラムの値にそれぞれ1を加える。
records.each {
r - & gt;
r[3] = (r[3].toInteger() + 1).toString()
}
([titles] + records).each {
println it.join("t")
}
|
ちょっと汚い。