gpt4 book ai didi

f# - FSharp - 基于数组的匹配和返回记录的意外行为

转载 作者:行者123 更新时间:2023-12-04 18:56:58 26 4
gpt4 key购买 nike

我刚刚真正进入 FSharp 并且我已经为此苦苦挣扎了很长一段时间,因为我不明白为什么我没有在比赛声明中得到预期的结果。

根据消息编号,我需要将数组(拆分)中的某些值与我想要返回的记录上的字段相匹配。

let UNMATCHED = "UNMATCHED"
let T0200 = "T0200" ;; let R0200 = "R0200"

let matchTypes = [| { MessageType = T0200; Regex = REGEX_T0200 }
; { MessageType = R0200; Regex = REGEX_R0200 }
...

let messageInfo (matchType:MatchType, line:string) =
let matching = Regex.Match(line, REGEX_DATA)
let splits = matching.Value.Replace("[","").Replace("]","").Split('|')
let showSplit (x:string[]) = "[" + String.Join(";",x) + "]"
let info =
Console.WriteLine("matching against: " + matchType.MessageType)
match matchType.MessageType with
| T0200 ->
{ MessageType = T0200; CustomerID = splits.[1]; CustomerName = "";
ItemID = splits.[2]; ItemDescription = "" }
| R0200 ->
{ MessageType = R0200; CustomerID = splits.[1]; CustomerName = splits.[2];
ItemID = splits.[5]; ItemDescription = splits.[6] }
...
| UNMATCHED -> { MessageType = UNMATCHED; CustomerID = ""; CustomerName = "";
ItemID = ""; ItemDescription = "" }
| _ -> { MessageType = UNMATCHED; CustomerID = ""; CustomerName = "";
ItemID = ""; ItemDescription = "" }
Console.WriteLine("messageInfo: {0}", (matchType, showSplit splits, info))
info

但是,这似乎不起作用,从下面的输出中,我得到了 T0200 的预期结果,但是对于 R0200,字段的映射与数组中的字段不匹配 - 即。它似乎使用与 T0200 语句相同的数组索引。我对所有其他后续消息都有完全相同的问题。
matching against: T0200
messageInfo: ({MessageType = "T0200";
Regex = "\sT0200\[([\w\s]*\|)*[\w\s]*\]";}, [3;000008757051;1401], {MessageType = "T0200";
CustomerID = "000008757051";
CustomerName = "";
ItemID = "1401";
ItemDescription = "";})
matching against: R0200
messageInfo: ({MessageType = "R0200";
Regex = "\sR0200\[([\w\s]*\|)*[\w\s]*\]";}, [3;000008757051;Ricky Bobby;413792;0;1401;SNACKER;909;0], {MessageType = "R0200";
CustomerID = "000008757051";
CustomerName = "";
ItemID = "Ricky Bobby";
ItemDescription = "";})

我猜我的 match 语句的语法有问题 - 我做错了什么?

我在 ubuntu 上运行 dotnet core 2.0.2

谢谢

最佳答案

MessageType是一个字符串和 T0200只是一个命名的字符串值。当您尝试在 T0200 上匹配时它实际上将字符串绑定(bind)到 T0200并且总是进入你的match的第一个分支.模式匹配语言不允许您匹配这样的值名称。您可以匹配有区别的联合案例、文字值和其他一些东西。见 here .

关于f# - FSharp - 基于数组的匹配和返回记录的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50372076/

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