Appending/adding 到具有两个值的映射
Appending/adding to a map with two values
我正在尝试在 Go 中创建一个映射,并根据从文件中读取的一段字符串中的正则表达式匹配为一个键分配两个值。
为此,我尝试使用两个 for 循环 - 一个分配第一个值,第二个分配下一个值(应该保持第一个值不变)。
到目前为止,我已经设法让正则表达式匹配工作,我可以创建字符串并将两个值之一放入地图中,或者我可以创建两个单独的地图,但这不是我想要的做。这是我使用的代码
package main
import (
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
)
type handkey struct {
game string
stake string
}
type handMap map[int]handkey
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
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(".+(\[=11=]\.05\/\[=11=]\.10)", slicedHands[i])
if matchStake {
mapHand[i] = handkey{stake: "10NL"}
}
}
fmt.Println(mapHand)
我尝试过的事情...1) 制作一个匹配两个表达式的 for 循环(无法解决)
2) 使用第一个值更新地图的第二个实例,因此两个值都放在第二个循环中(无法解决)
我理解它会再次重新创建地图,并且没有分配第一个值。
试试这个:
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(".+(\[=10=]\.05\/\[=10=]\.10)", slicedHands[i])
h := handkey{}
if matchHoldem {
h.game = "No Limit Hold'em"
}
if matchStake {
h.stake = "10NL"
}
mapHand[i] = h
}
}
我正在尝试在 Go 中创建一个映射,并根据从文件中读取的一段字符串中的正则表达式匹配为一个键分配两个值。
为此,我尝试使用两个 for 循环 - 一个分配第一个值,第二个分配下一个值(应该保持第一个值不变)。
到目前为止,我已经设法让正则表达式匹配工作,我可以创建字符串并将两个值之一放入地图中,或者我可以创建两个单独的地图,但这不是我想要的做。这是我使用的代码
package main
import (
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
)
type handkey struct {
game string
stake string
}
type handMap map[int]handkey
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
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(".+(\[=11=]\.05\/\[=11=]\.10)", slicedHands[i])
if matchStake {
mapHand[i] = handkey{stake: "10NL"}
}
}
fmt.Println(mapHand)
我尝试过的事情...1) 制作一个匹配两个表达式的 for 循环(无法解决) 2) 使用第一个值更新地图的第二个实例,因此两个值都放在第二个循环中(无法解决)
我理解它会再次重新创建地图,并且没有分配第一个值。
试试这个:
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(".+(\[=10=]\.05\/\[=10=]\.10)", slicedHands[i])
h := handkey{}
if matchHoldem {
h.game = "No Limit Hold'em"
}
if matchStake {
h.stake = "10NL"
}
mapHand[i] = h
}
}