dtrz17917 2019-05-13 06:42
浏览 38
已采纳

内部循环定义父链接

I would like to loop on the name of my tables to add the associations defined by the symbol "_".

If table a_b and a then b exists then a = [b], b = [a]. Finally, I do not have to print the tables containing a "_" in the name

Struct

// Table with Fields and Assoc
type Table struct {
    Name       string
    Assoc      []Assoc
}

// Assoc is a name of associated Table
type Assoc struct {
  Name string
}
tables := []string{
    "a",
    "b",
    "c",
    "d",
    "f",
    "a_b",
    "a_c",
    "a_d_f",
    "c_d",      
}

var tbls []Table

for _, t := range tables {
    if strings.Contains(t, "_") {
        // Split to find "_" like assoc := strings.Split(t, "_") ?
        // append in struct "Table{Name:a, Assoc:  [b,c,d,f]}"
        // append in struct "Table{Name:b, Assoc:  [a]}"
        // append in struct "Table{Name:c, Assoc:  [a,d]}"
        // append in struct "Table{Name:d, Assoc:  [a,c,f]}"
        // append in struct "Table{Name:f, Assoc:  [a,d]}"      
    } else {
        n := Table{
            Name: t,
        }
        tbls = append(tbls, n)
    }

}

Return like fmt.Println(tbls) :

[{a [b,c,d,f]} {b [a]} {c [a,d]} {d [a,c,f]} {f [a,d]}]

Go Playground

  • 写回答

1条回答 默认 最新

  • dslkchyv673627 2019-05-13 07:09
    关注

    Done the above mentioned using map https://play.golang.org/p/8C5M0L-es6o

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    // Table with Fields and Assoc
    type Table struct {
        Name  string
        Assoc map[string]int
    }
    
    // Assoc is a name of associated Table
    // type Assoc struct {
    //  Name string
    // }
    
    func main() {
        tables := []string{
            "a",
            "b",
            "c",
            "d",
            "f",
            "a_b",
            "a_c",
            "a_d_f",
            "c_d",
        }
    
        var tbls = make(map[string]map[string]int)
    
        for _, t := range tables {
            if strings.Contains(t, "_") {
                splitAssocs := strings.Split(t, "_")            
                for i:=0;i<=len(splitAssocs)-2;i++ {
                    for j:=(i+1);j<=len(splitAssocs)-1;j++{
                        _, ok := tbls[splitAssocs[i]]
                        if !ok{
                            tbls[splitAssocs[i]] = make(map[string]int)
                        }
                         _, ok = tbls[splitAssocs[j]]
                        if !ok{
                            tbls[splitAssocs[j]] = make(map[string]int)
                        }
                        tbls[splitAssocs[i]][splitAssocs[j]] = 1
                        tbls[splitAssocs[j]][splitAssocs[i]] = 1
                    }
    
                }
    
            } else {
                _, ok := tbls[t]            
                if !ok{
                    tbls[t] = make(map[string]int)
                }
            }
    
        }
    
        fmt.Println(tbls)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算