Split an array into multiple arrays by value

Hello, I have such a task, please help me figure it out, it is necessary to split the array into several arrays, despite the fact that the first element of the new arrays starts with 101, followed by the rest of the elements with 000 of which there can be as many as you like (maybe not at all) , in general, it is necessary to break it down by values starting with 101, (101 is a new array, followed by elements from 000 and then 101 again a new array) How to properly organize the iteration cycle I cannot understand, please help me figure it out

The array itself

`````` func main() {
var rs = []string{"101 HASH", "000 HASHEDLSVALUES", "000 HASHEDPROCCVALUES", "101 NEWHASH", "000 HASHEDLSVALUES",
"000 HASHEDPRVALUES", "000 HASHEDIVAVALUES", "101 SECNEWHASH", "000 HASHEDPRVALUES", "000 HASHEDIVAVALUES",
"101 NEWPROHASH", "000 HASHEDPRVALUES", "000 HASHEDIVAVALUES", "000 HASHEDPRVALUES", "000 HASHEDIVAVALUES",
"000 HASHEDPRVALUES", "000 HASHEDIVAVALUES", "101 HOPENEWHASH", "000 HASHEDPRVALUES", "000 HASHEDIVAVALUES",
"000 HASHEDPRVALUES", "000 HASHEDIVAVALUES"}
// how to do the iteration correctly we do not understand only I find the first match
var arr []string
for i := 0; i < len(rs); i++ {
if rs[i][0] != 49 {
arr = append(arr, rs[i])
}
if rs[i+1][0] == 49 {
break
}
}
fmt.Println(arr)
}``````
1 Like

Your values are starting in `rs` which is of type `[]string`. `[]string` means a slice (sequence) of `string`s. If I understand your question correctly, you don’t want all of the results back in a single sequence of strings at the end; you probably are looking for a sequence of sequences of strings, the first “subsequence” containing the first batch of values, then the second “subsequence” containing the second, etc.

Just as the type for a slice of strings is `[]` + `string`, the type for a slice of slices of strings is `[]` + `[]string` = `[][]string`, so I believe `arr` should be of type `[][]string` so that you know where the subsequences start and end.

The next step I would take is to think of how to describe the steps in “pseudocode,” or code in some imaginary language that you should be able to “translate” into real code after you work out the steps. I would start off with pseudocode like this:

``````For each element, r, in rs:
- If r starts with "101":
- Create a new []string slice that we're going to put the elements into
- For each element, r2, after r in rs:
- If r starts with "101":
- Break out of this inner loop because the 101 means we're starting
a new sequence.
- Else r does not start with 101, so append r2 to the sequence we
created on pseudocode line 3
- Append our []string slice to the slice-of-slices, arr.
- Else we're at the first element in rs but it doesn't start with "101"
What should we do now?
``````

With the steps written out, I think it can more easily be translated into Go. I will say that it could be easier than how I wrote it out, but I think that it’s a start.

Greetings, skillian

It is not entirely clear that in the last condition there should be an else, data from 101 is distributed across arrays, but data from 100 is not added to these arrays

``````var vs [][]string

for k, r := range rs {
if strings.HasPrefix(r, "101") {
sr := []string{}
for _, r2 := range rs[k] {
if strings.HasPrefix(string(r2), "101") {
continue
} else {
sr = append(sr, string(r2))
}
}
vs = append(vs, sr)
} else {
/* here it turns out, when executed, all data with 000
is displayed, that I need to distribute into arrays
with data 101 */
}
}
``````

Your question is vague. It would help a lot if you provided expected output.

Is this the expected output?

``````["101 HASH" "000 HASHEDLSVALUES" "000 HASHEDPROCCVALUES"]
["101 NEWHASH" "000 HASHEDLSVALUES" "000 HASHEDPRVALUES" "000 HASHEDIVAVALUES"]
["101 SECNEWHASH" "000 HASHEDPRVALUES" "000 HASHEDIVAVALUES"]
["101 NEWPROHASH" "000 HASHEDPRVALUES" "000 HASHEDIVAVALUES" "000 HASHEDPRVALUES" "000 HASHEDIVAVALUES" "000 HASHEDPRVALUES" "000 HASHEDIVAVALUES"]
["101 HOPENEWHASH" "000 HASHEDPRVALUES" "000 HASHEDIVAVALUES" "000 HASHEDPRVALUES" "000 HASHEDIVAVALUES"]
``````

.

``````package main

import (
"fmt"
"strings"
)

func subslices(slice []string) [][]string {
var ss [][]string
for _, e := range slice {
if strings.HasPrefix(e, "101") || len(ss) == 0 {
ss = append(ss, make([]string, 0, 3))
}
end := len(ss) - 1
ss[end] = append(ss[end], e)
}
return ss
}

func main() {
var rs = []string{
"101 HASH", "000 HASHEDLSVALUES", "000 HASHEDPROCCVALUES",
"101 NEWHASH", "000 HASHEDLSVALUES", "000 HASHEDPRVALUES",
"000 HASHEDIVAVALUES",
"101 SECNEWHASH", "000 HASHEDPRVALUES", "000 HASHEDIVAVALUES",
"101 NEWPROHASH", "000 HASHEDPRVALUES", "000 HASHEDIVAVALUES",
"000 HASHEDPRVALUES", "000 HASHEDIVAVALUES", "000 HASHEDPRVALUES",
"000 HASHEDIVAVALUES",
"101 HOPENEWHASH", "000 HASHEDPRVALUES", "000 HASHEDIVAVALUES",
"000 HASHEDPRVALUES", "000 HASHEDIVAVALUES",
}

for _, sub := range subslices(rs) {
fmt.Printf("%q\n", sub)
}
}
``````

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

1 Like

Yes, that’s right, I’m sorry, my English is still not good enough, maybe I put it wrong somewhere. Thanks for helping me

Hi @n0kk

``````        var ar []string
var arr [][]string
for _, i := range rs {
if i[0] == 49 {
if len(ar) > 0 {
arr = append(arr, ar)
//fmt.Println(ar)
}
ar = append([]string{}, i)
continue
}
ar = append(ar, i)
}
arr = append(arr, ar)
//fmt.Println(ar)
``````
1 Like

Thanks

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