的重复图案我有如下的布局文件的字符串:含有重新排列可变长度
TABLE name_of_table
COLUMNS FIRST_COLUMN 2nd_column [..]正th_column
VALUES 1st_value 2nd_value [ ...]第n个值
VALUES yet_another_value ...继续
开始从另一个表重复.....
我想有这个文本文件重新安排到了我,所以我没有在每一个VALUES行的前面键入表和列,得到以下特性:
TABLE name_of_table COLUMNS FIRST_COLUMN [..]的第n列的值1st_value
TABLE name_of_table COLUMNS FIRST_COLUMN [..]的第n列的值yetanother_value
我需要输入一次在这里重新排列的几行,因此让整个文本文件与hGetContents字符串似乎是恰当的,产生的字符串是这样的:
表name_of_table柱FIRST_COLUMN [ ..] n-th_column VALUES 1st_value [..] n-th_value VALUES another_value [..] yet_another VALUES ...... ANOTHER TABLE .... COLUMNS .... VALUES [....] VALUES .. 。
我已经试过这样做了嵌套的情况下,递归。这给我一个困境,我需要帮助:
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,价值观BLA BLA BLA VALUES BLA,我不能再FIRSTPART取,创造FIRSTPART,价值观,FIRSTPART, VALUES,FIRSTPART,VALUES。
试图通过在valueRecursion中引用myStr来做到这一点显然超出了范围。
怎么办?
好像您需要采取双管齐下的方法 - 将输入解析为理智的数据结构,然后遍历数据结构以产生消除输出。我会看看我是否找到一个优雅的解决方案。 – jrockway 2010-08-03 03:16:07