gpt4 book ai didi

xml - 构建 map [String,String]

转载 作者:行者123 更新时间:2023-12-04 18:13:16 25 4
gpt4 key购买 nike

我有以下功能。它可以很好地打印我要存储的值。我想建立一个 Map[String, String]

def storePlayerEntry(n: Node, moved: Boolean) = {

moved match {
// storing a current player record
// rowkey = playerid
case true => {
//println("current: " + teamId + ":" + n)
println("id: " + n \ "@uID")
for (
s <- Seq("first_name",
"last_name",
"known_name",
"birth_date",
"weight",
"height",
"jersey_num",
"real_position",
"real_position_side",
"join_date",
"country")
) {
val stat = (n \\ "Stat").filter(_.attribute("Type").filter(_.toString() == s).isDefined)
Option(stat.text) match {
case Some(st) if (st == "") => // do nothing
case Some(st) => println(st)
case None => // do nothing
}
}
//val ret = dataApi.upsertMulti("players",
}
case false => {
//println("old: " + teamId + ":" + n)
}
}
}

我避免声明 map 然后只是添加方法,因为我确信这里有更多的 scala-ish 要做。

我正在寻找的 map 将包含类似 s -> st

最佳答案

@jdevelop 很接近。应该是这样的:

val x = (for (
s <- Seq("first_name",
"last_name",
"known_name",
"birth_date",
"weight",
"height",
"jersey_num",
"real_position",
"real_position_side",
"join_date",
"country");
val st = (n \\ "Stat").filter(_.attribute("Type").filter(_.toString() == s).isDefined).text if (st != "")
) yield (s -> st)).toMap

关于xml - 构建 map [String,String],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12206675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com