Making a tree of specific indices based on the original tree of strings

Hello there,
I have been struggling to make a tree based on another with searching for a specific item in the tree and finding out about the indices of those in the same tree structure but so far I couldn’t find an efficient way to do it with a c# component. Can anyone help me with this?
My code is:

private void RunScript(string x, DataTree<string> y, ref object A)
  {
    DataTree <int> stringTree = new DataTree<int>();
    var paths = y.Paths;
    foreach( var j in paths)
    {
      var newpath = stringTree.EnsurePath(j);
      foreach ( var k in stringTree.Paths)
      {
        if (k == j)
        {
          foreach (List<string> branch in y.Branches)
          {
            List<int> newLists = new List<int>();
            foreach (var i in branch)
            {
              var text = i.ToString();
              var index = branch.IndexOf(text);
              if (text.Contains(x))
              {
                newLists.Add(index);
                stringTree.AddRange(newLists, k);
              }
            }
          }
        }
      }
    }

    A = stringTree;


as you can see I get wrong result. It would be nice if someone tells me where I am going wrong.
Thanks alot!
questionC#GH.gh (9.9 KB)

See attached

GetIndices_V1.gh (9.4 KB)

But … I would strongly advise to use a suitable Class for that kind of stuff (then LINQ this, LINQ that etc etc).

1 Like

Thank you So much Peter!

Notify if you need some far more complex stuff (i.e. queries [LINQ] and the likes).

1 Like