【发布时间】:2010-08-03 03:08:06
【问题描述】:
我有一个布局如下的文件:
表名_of_table
COLUMNS first_column 2nd_column [..] n-th_column
VALUES 1st_value 2nd_value [...] 第 n 个值
VALUES yet_another_value ...继续
ANOTHER TABLE 从头开始重复.....
我想为我重新排列这个文本文件,所以我不必在每个 VALUES 行前面输入 TABLE 和 COLUMNS,从而产生:
TABLE name_of_table COLUMNS first_column [..] n-th column VALUES 1st_value
TABLE name_of_table COLUMNS first_column [..] n-th column VALUES yetanother_value
我需要在这里一次输入并重新排列几行,因此使用 hGetContents 将整个文本文件作为字符串获取似乎是合适的,产生如下字符串:
TABLE name_of_table COLUMNS first_column [..] n-th_column VALUES 1st_value [..] n-th_value VALUES another_value [..] yet_another VALUES ...... ANOTHER TABLE .... COLUMNS .... VALUES [ ....]价值观 ...
我已经尝试使用嵌套的 case of's 和递归来做到这一点。这给了我一个需要帮助的困境:
1) 我需要递归以避免无休止的大小写嵌套问题。
2) 使用递归,我不能替代添加字符串的前面部分,因为递归只引用字符串的尾部!
说明问题:
myStr::[[Char]]->[[Char]]
myStr [] = []
myStr one =
case (head one) of
"table" -> "insert into":(head two):columnRecursion (three) ++ case (head four) of
"values" -> (head four):valueRecursion (tail three) ++ myStr (tail four)
_ -> case head (tail four) of
"values" -> (head (tail four):myStr (tail (tail four))
_ ->
where two = tail one
three = tail two
four = tail three
columnRecursion::[[Char]] -> [[Char]]
columnRecursion [] = []
columnRecursion cool =
case (head cool) of
"columns" -> "(":columnRecursion (tail cool)
"values" -> [")"]
_ -> (head cool):columnRecursion (tail cool)
valueRecursion::[[Char]] -> [[Char]]
valueRecursion foo =
case head foo of
"values" -> "insert into":(head two):columnRecursion (three) ++ valueRecursion (tail foo)
"table" -> []
"columns"-> []
_ -> (head foo):valueRecursion (tail foo)
我以 FIRSTPART, VALUES bla bla VALUES bla bla 结束,但我无法再次获取 FIRSTPART 来创建 FIRSTPART, VALUES, FIRSTPART, VALUES, FIRSTPART, VALUES。
试图通过在 valueRecursion 中引用 myStr 来做到这一点显然超出了范围。
怎么办??
【问题讨论】:
-
似乎您需要采取两管齐下的方法——将输入解析为合理的数据结构,然后遍历数据结构以生成经过处理的输出。我会看看我是否找到了一个优雅的解决方案。