Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

go - Appending/adding to a map with two values

I'm trying to create a map in Go and assign two values to one key based on a regex match within a slice of strings which are read from a file.

To do this I'm trying to use two for loops - one to assign the first value and the second to assign the next value (which should leave the first value unaltered).

So far I've managed to get the regex match working and I can create the string and put either one of the two values into the map, or I can create two seperate maps, but this isnt what I intend to do. Heres the code Im using

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, "


") //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)

Things I've tried...1) making one for loop which matches both expressions (couldnt solve) 2) Using the first value to update the second instance of the map so both values are put there in the second loop (couldnt solve)

I understand its recreating the map again and without the first value assigned.

question from:https://stackoverflow.com/questions/65833012/appending-adding-to-a-map-with-two-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

func main() {

    file, rah := ioutil.ReadFile("HH20201223.txt")
    if rah != nil {
        log.Fatal(rah)
    }
    str := string(file)

    slicedHands := strings.SplitAfter(str, "


") //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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...