I'm trying to parse a file of this forme:
12
0,1,2,3,1,2,3,4,2,3,4,5
1,0,1,2,2,1,2,3,3,2,3,4
2,1,0,1,3,2,1,2,4,3,2,3
3,2,1,0,4,3,2,1,5,4,3,2
1,2,3,4,0,1,2,3,1,2,3,4
2,1,2,3,1,0,1,2,2,1,2,3
3,2,1,2,2,1,0,1,3,2,1,2
4,3,2,1,3,2,1,0,4,3,2,1
2,3,4,5,1,2,3,4,0,1,2,3
3,2,3,4,2,1,2,3,1,0,1,2
4,3,2,3,3,2,1,2,2,1,0,1
5,4,3,2,4,3,2,1,3,2,1,0
0,5,2,4,1,0,0,6,2,1,1,1
5,0,3,0,2,2,2,0,4,5,0,0
2,3,0,0,0,0,0,5,5,2,2,2
4,0,0,0,5,2,2,10,0,0,5,5
1,2,0,5,0,10,0,0,0,5,1,1
0,2,0,2,10,0,5,1,1,5,4,0
0,2,0,2,0,5,0,10,5,2,3,3
6,0,5,10,0,1,10,0,0,0,5,0
2,4,5,0,0,1,5,0,0,0,10,10
1,5,2,0,5,5,2,0,0,0,5,0
1,0,2,5,1,4,3,5,10,5,0,2
1,0,2,5,1,0,3,0,10,0,2,0
Where the first line give us the size of the matrix: n x n
The n-following lines are the matrix D. Then the n-following lines are the matrix W. So there is 2n + 1
lines.
Here is the code to parse it and put it in variables.
func readFile(path string) (int64, Matrix, Matrix) {
// open the file
f, _ := os.Open(path)
defer f.Close()
// init the new reader on the opened file
r := bufio.NewReader(f)
// we get the n value
line, _ := r.ReadString('\n')
splitedLine := strings.Fields(line)
tmp, _ := strconv.ParseInt(splitedLine[0], 10, 64)
n := int64(tmp)
// we init the matrix W and D
D := Matrix{}
D.matrix = make([][]int64, n)
for i, _ := range D.matrix {
D.matrix[i] = make([]int64, n)
}
W := Matrix{}
W.matrix = make([][]int64, n)
for i, _ := range W.matrix {
W.matrix[i] = make([]int64, n)
}
// loop on the n first element
iter := int64(0)
for iter < n {
// we get the n following elements
line, _ = r.ReadString('\n')
for index, ele := range strings.Split(line, ",") {
D.matrix[iter][index], _ = strconv.ParseInt(ele, 10, 64)
}
iter++
}
iter = 0
for iter < n {
// we get the n following elements
line, _ = r.ReadString('\n')
for index, ele := range strings.Split(line, ",") {
W.matrix[iter][index], _ = strconv.ParseInt(ele, 10, 64)
}
iter++
}
return n, W, D
}
The resultat for W should be like:
[ 0., 1., 2., 3., 1., 2., 3., 4., 2., 3., 4., 5.],
[ 1., 0., 1., 2., 2., 1., 2., 3., 3., 2., 3., 4.],
[ 2., 1., 0., 1., 3., 2., 1., 2., 4., 3., 2., 3.],
[ 3., 2., 1., 0., 4., 3., 2., 1., 5., 4., 3., 2.],
[ 1., 2., 3., 4., 0., 1., 2., 3., 1., 2., 3., 4.],
[ 2., 1., 2., 3., 1., 0., 1., 2., 2., 1., 2., 3.],
[ 3., 2., 1., 2., 2., 1., 0., 1., 3., 2., 1., 2.],
[ 4., 3., 2., 1., 3., 2., 1., 0., 4., 3., 2., 1.],
[ 2., 3., 4., 5., 1., 2., 3., 4., 0., 1., 2., 3.],
[ 3., 2., 3., 4., 2., 1., 2., 3., 1., 0., 1., 2.],
[ 4., 3., 2., 3., 3., 2., 1., 2., 2., 1., 0., 1.],
[ 5., 4., 3., 2., 4., 3., 2., 1., 3., 2., 1., 0.]
But when I print it; it gives me :
[0 1 2 3 1 2 3 4 2 3 4 0]
[1 0 1 2 2 1 2 3 3 2 3 0]
[2 1 0 1 3 2 1 2 4 3 2 0]
[3 2 1 0 4 3 2 1 5 4 3 0]
[1 2 3 4 0 1 2 3 1 2 3 0]
[2 1 2 3 1 0 1 2 2 1 2 0]
[3 2 1 2 2 1 0 1 3 2 1 0]
[4 3 2 1 3 2 1 0 4 3 2 0]
[2 3 4 5 1 2 3 4 0 1 2 0]
[3 2 3 4 2 1 2 3 1 0 1 0]
[4 3 2 3 3 2 1 2 2 1 0 0]
[5 4 3 2 4 3 2 1 3 2 1 0]
I don't know why. Thanks for your reply.