如何将 Lua 中的列表转换为 Kotlin?

我正在一个最初从 Lua 开始的项目上工作,并希望将其更新到 Kotlin。

我有大约 5000 个类似以下形式的问题/答案:

['Question'][1] = "'7x' 是哪种饮料的秘密成分?";
['Answers'][1] = {"可口可乐"};
['Question'][2] = "'And the big wheel keep on turning neon burning up above and I'm just high on the world come on and take the low ride with me girl on the.....' 这首 Dire Straits 歌曲的歌名是什么?";
['Answers'][2] = {"爱的隧道"};

我想重新格式化这些问题,而不是手动查看所有 5000 个问题,以便它们看起来像这样:

val que1 = Question(
   1, "'7x' 是哪种饮料的秘密成分?",
"可口可乐"
        )
val que2 = Question(
            2, "'And the big wheel keep on turning neon burning up above and I'm just high on the world come on and take the low ride with me girl on the.....' 这首 Dire Straits 歌曲的歌名是什么?",
"爱的隧道"
        )

请帮助我找出如何重新格式化这些问题/答案。谢谢。

原文链接 https://stackoverflow.com/questions/70373490

点赞
stackoverflow用户15057647
stackoverflow用户15057647

你可以编写类似这样的脚本来获取所需的输出。

步骤1:创建 input.txt 并在其中粘贴您想要翻译的 lua 代码。

['Question'][1] = "'7x' was used to refer to the secret ingredient of what drink";
['Answers'][1] = {"coca cola"};
['Question'][2] = "'And the big wheel keep on turning neon burning up above and I'm just high on the world come on and take the low ride with me girl on the.....' What's the Dire Straits song title?";
['Answers'][2] = {"tunnel of love"};

步骤2:创建一个名为 output.txt 的空文件。此文件将包含转换后的代码。

步骤3:运行此 main 函数。您可能需要根据您的目录结构修改文件路径。注意:此代码是用 Kotlin 编写的。

fun main() {
    val input = File("src/main/kotlin", "input.txt").readLines()
    val outputWriter = File("src/main/kotlin", "output.txt").printWriter()

    val lines = input.windowed(2, 2)
    val questions = mutableListOf<String>()
    lines.forEachIndexed { index, str ->
        val que = str[0].split("\"")[1]
        val ans = str[1].split("\"")[1]

        val question = "val que${index + 1} = Question(${index + 1}, \"$que\", \"$ans\")"
        println(question)
        questions.add(question)

    }

    outputWriter.use { out->
        questions.forEach {
            out.println(it)
        }
    }
}

运行脚本后,您将在 output.txt 中获得所需的输出。或者,您也可以从控制台中获取输出。

2021-12-16 05:48:26