From 8e4a48825ae67f61973922e836d4180dc576be97 Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Wed, 13 Jun 2018 10:26:16 -0700 Subject: acronym.go: generalizing first-letter indexing --- go/acronym.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'go') diff --git a/go/acronym.go b/go/acronym.go index 955f256..ef92ef4 100644 --- a/go/acronym.go +++ b/go/acronym.go @@ -8,7 +8,7 @@ import( "net" "os" "regexp" - "strings" + //"strings" "time" ) @@ -26,19 +26,9 @@ func main() { defer dictfile.Close() scanner := bufio.NewScanner( dictfile ) scanner.Split(bufio.ScanLines) - var clines []string - var blines []string - var klines []string + dict := make( map[string][]string ) for scanner.Scan() { - if strings.HasPrefix( scanner.Text(), "c" ) { - clines = append(clines, scanner.Text()) - } - if strings.HasPrefix( scanner.Text(), "b" ) { - blines = append(blines, scanner.Text()) - } - if strings.HasPrefix( scanner.Text(), "k" ) { - klines = append(klines, scanner.Text()) - } + dict[scanner.Text()[:1]] = append(dict[scanner.Text()[:1]], scanner.Text()) } r := regexp.MustCompile( "^[0-9]{2}:[0-9]{2} <(.bungmonkey|.cornbugs|.wishapb|@[a-zA-Z0-9_]+)> +!cbk" ) @@ -52,7 +42,7 @@ func main() { } if r.MatchString( logline.Text ) { var cbk string - cbk = fmt.Sprintf( "%s %s %s\n", clines[ rand.Intn( len( clines ) ) ], blines[ rand.Intn( len( blines ) ) ], klines[ rand.Intn( len( klines ) ) ] ) + cbk = fmt.Sprintf( "%s %s %s\n", dict["c"][ rand.Intn( len( dict["c"] ) ) ], dict["b"][ rand.Intn( len( dict["b"] ) ) ], dict["k"][ rand.Intn( len( dict["k"] ) ) ] ) fmt.Printf( "%v", cbk ) ircsocket, err := net.Dial( "unix", os.ExpandEnv( "${HOME}/.irssi/twitch-cornbugs-socket" ) ) check( err ) -- cgit v1.2.3