我正在尝试从字符串中提取一些值。该字符串包含几行带有值的行。每行的值是数字、名字、姓氏。然后我想按给定的模式过滤并删除重复的数字。
这是我的测试:
test("Numbers should be unique") {
val s = Cool.prepareListAccordingToPattern(ALLOWED_PATTERN, "1234,örjan,nilsson\n4321,eva-lisa,nyman\n1234,eva,nilsson")
assert(s.length == 2, "Well that didn't work.. ")
info("Chopping seems to work. Filtered duplicate numbers. Expected 1234:4321, got: "+s(0)(0)+":"+s(1)(0))
}
方法:
def prepareListAccordingToPattern(allowedPattern: String, s: String) : Array[Array[String]] = {
val lines = chop("\n", s)
val choppedUp = lines.map(line =>
chop(",", line)).filter(array =>
array.length == 3 && array(0).matches(allowedPattern)
)
choppedUp
}
def chop(splitSymbol: String, toChop: String) : Array[String] = {
toChop.split(splitSymbol)
}
我的测试按预期失败,因为我收到了一个带有重复项的多维数组:
[0]["1234","örjan","尼尔森"]
[1]["4321","eva-lisa","nyman"]
[2][“1234”,“伊娃”,“尼尔森”]
我想做的是过滤掉重复的数字,在这种情况下是“1234” ,以便我回来:
[0]["1234","örjan","尼尔森"]
[1]["4321","eva-lisa","nyman"]
我应该如何以scala方式做到这一点?也许我可以以不同的方式解决这个问题?