Comment detail

RFC 4180対応版 CSVレコードの分解 (Nested Flatten)
Squeak Smalltalk で。

手近に CSV 解析器が見あたらなかったので、よく似た作業をする
Smalltalk 処理系の字句解析器のインスタンスをハックして
なんちゃって CSV 解析器(^_^;)を仕立ててみました。

具体的には、カンマをデリミタに、スペースを通常の文字に、
CR を閉じ括弧、LF を開く括弧に見立てるよう、スキャナのテーブルを
書き換え騙して仕事をさせます。なお、ダブルクオートは Smalltalk では
コメントアウトになってしまうので、文字列リテラルを表す
シングルクオートに差し替え、解析後、ダブルクオートに戻しています。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
| scanner typeTable data dataFile dataString |
scanner := Scanner new.
typeTable := scanner instVarNamed: #typeTable.
typeTable := scanner instVarNamed: #typeTable put: typeTable copy.
typeTable at: $, asciiValue put: #xDelimiter.
typeTable at: $  asciiValue put: #xLetter.
typeTable at: Character lf asciiValue put: #leftParenthesis.
typeTable at: Character cr asciiValue put: #rightParenthesis.
dataFile := FileStream fileNamed: 'data.txt'.
dataString := dataFile contents replaceAll: $" with: $'; copyWithFirst: $(.
data := scanner scanTokens: dataString.
World findATranscript: nil.
data do: [:record |
    record doWithIndex: [:field :index |
        field replaceAll: $' with: $".
        field := field copyWithout: Character lf.
        Transcript cr; show: ('{1} => {2}' format: {index. field})]]

Index

Feed

Other

Link

Pathtraq

loading...