Get data joined from all map-array elements

Hi,

Please help me write in GO such code (pseudo Perl):

foreach i (@{resultArray}){
int c1 = 0;
foreach j (@{i}) {
int c2 = 0 ;
foreach n (@{j}){
print $i[c1], $j[c2], $n;
}
c2++;
}
c1++;

Here’s some my tries to write it:
Playground

In the end I need get array like:

[0] => (20, nano, Valdlauchi)
[1] => (20, small, Valdlauchi)
[2] => (20, medium, Valdlauchi)
[3] => (60, nano, Valdlauchi)
[4] => (60, small, Valdlauchi)
[5] => (60, medium, Valdlauchi)

Thanks!

Is this the desired effect your looking for?

https://play.golang.org/p/mXr0eToJf0

Hi,

Thank you for reply!

Not quite. I need chain with every element in all dimensions. But every chain should be unique between their selfs.

Like this:

[0] => (20, nano, Valdlauchi)
[1] => (20, small, Valdlauchi)
[2] => (20, medium, Valdlauchi)
[3] => (60, nano, Valdlauchi)
[4] => (60, small, Valdlauchi)
[5] => (60, medium, Valdlauchi)

All 6 pairs/chains are unique but together 6 records are all possible options from such map:

mom := map[string]map[int]pStruct{
“disk”: map[int]pStruct{0: {“20”}, 1: {“60”}},
“service”: map[int]pStruct{0: {“nano”}, 1: {“small”}, 2: {“medium”}},
“domain”: map[int]pStruct{0: {“Valdlauchi”}},
}

https://play.golang.org/p/r40hpoS5LW ?

Yeap, it works as expected! Thank you! :+1:

But can It be without “hardcoded” map element name like “disk” etc?

Sure. This becomes the usual problem of calculating all the combinations from a set of lists. Here’s one solution: https://play.golang.org/p/51lg3hrePz

But now we’re just talking general programming and algorithms and not Go specifically. Are you trying to learn Go? If so, the Go Tour is a good starting point and there are many other resources, probably can be found by searching the forum here or Google. For general algorithm and procedures there are courses and books… Is this what you’re studying?

Thank you!

You are almost right :slight_smile:

I hadn’t any experience in GO before, but I have concrete task that I must resolve, create small automation tool - and this part with parsing data correctly was stumbling block that I couldn’t get trough because of luck of knowledge. For all other things I’m getting info and learning from sources you mentioned.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.