亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

追加/添加到具有兩個值的地圖

追加/添加到具有兩個值的地圖

Go
撒科打諢 2022-08-01 17:27:11
我正在嘗試在Go中創建一個映射,并根據從文件中讀取的字符串切片中的正則表達式匹配為一個鍵分配兩個值。為此,我嘗試使用兩個for循環 - 一個用于分配第一個值,另一個用于分配下一個值(這應該使第一個值保持不變)。到目前為止,我已經設法使正則表達式匹配工作,我可以創建字符串并將兩個值中的任何一個放入映射中,或者我可以創建兩個單獨的映射,但這不是我打算做的。這是代碼 Im 使用package mainimport (    "fmt"    "io/ioutil"    "log"    "regexp"    "strings")type handkey struct {    game  string    stake string}type handMap map[int]handkeyfunc main() {    file, rah := ioutil.ReadFile("HH20201223.txt")    if rah != nil {        log.Fatal(rah)    }    str := string(file)    slicedHands := strings.SplitAfter(str, "\n\n\n") //splits the string after two new lines, hands is a slice of strings    mapHand := handMap{}    for i := range slicedHands {        matchHoldem, _ := regexp.MatchString(".+Hold'em", slicedHands[i]) //This line matches the regex        if matchHoldem {                                                  //If statement does something if it's matched            mapHand[i] = handkey{game: "No Limit Hold'em"} //This line put value of game to key id 'i'        }    }    for i := range slicedHands {        matchStake, _ := regexp.MatchString(".+(\\$0\\.05\\/\\$0\\.10)", slicedHands[i])        if matchStake {            mapHand[i] = handkey{stake: "10NL"}        }    }    fmt.Println(mapHand)我嘗試過的事情...1)制作一個與兩個表達式匹配的for循環(無法求解) 2)使用第一個值更新映射的第二個實例,以便將兩個值都放在第二個循環中(無法求解)我理解它再次重新創建地圖,并且沒有分配第一個值。
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

試試這個:


func main() {


    file, rah := ioutil.ReadFile("HH20201223.txt")

    if rah != nil {

        log.Fatal(rah)

    }

    str := string(file)


    slicedHands := strings.SplitAfter(str, "\n\n\n") //splits the string after two new lines, hands is a slice of strings


    mapHand := handMap{}


    for i := range slicedHands {

        matchHoldem, _ := regexp.MatchString(".+Hold'em", slicedHands[i]) //This line matches the regex

        matchStake, _ := regexp.MatchString(".+(\\$0\\.05\\/\\$0\\.10)", slicedHands[i])


        h := handkey{}

        if matchHoldem {

            h.game = "No Limit Hold'em"

        }

        if matchStake {

            h.stake = "10NL"

        }

        mapHand[i] = h

    }

}


查看完整回答
反對 回復 2022-08-01
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號