Skip to content

Instantly share code, notes, and snippets.

@prasanthabr
Created January 13, 2023 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasanthabr/fbfd16209797427de7f37ee85990597e to your computer and use it in GitHub Desktop.
Save prasanthabr/fbfd16209797427de7f37ee85990597e to your computer and use it in GitHub Desktop.
Custom Find based on Identified Index
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"sort"
)
func main() {
programmap("ace.csv", "AK1305", 1, 0)
}
func programmap(file string, prog string, course int, student int) {
var header []string
cxmap := make(map[string][]string) //map of student
var stds []string
cxfinal := [][]string{}
count := 0
csvFile, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
f := csv.NewReader(csvFile)
for {
if count > 50000 {
break
}
record, err := f.Read()
if err == io.EOF {
break
}
count++
if err != nil {
log.Fatal(err)
}
if header == nil {
header = record
continue
}
cxmap[record[student]] = append(cxmap[record[student]], record[course])
if record[course] == prog {
stds = append(stds, record[student])
fmt.Println(cxmap[record[student]])
}
}
sort.Strings(stds)
for _, v := range stds {
temp := make([]string, 0)
temp = append(temp, v)
sort.Strings(cxmap[v])
temp = append(temp, cxmap[v]...)
cxfinal = append(cxfinal, [][]string{temp}...)
}
writecsv(cxfinal)
}
func writecsv(rows [][]string) {
csvfile, err := os.Create("test.csv")
if err != nil {
log.Fatalf("failed creating file: %s", err)
}
csvwriter := csv.NewWriter(csvfile)
for _, row := range rows {
_ = csvwriter.Write(row)
}
csvwriter.Flush()
csvfile.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment