Conditional branch merging based on data values (using VB script)

Hi everyone,

I’m a total newbie to grasshopper and VB (started less than a week ago), so please bear with me if I’m being dumb.

I have a tree with different values of data within it (see image attached). All I want to do using the VB script is to create a new tree structure which has branches based on data values (for the mentioned example, I would like to two branches with data going to branch 1 if the values are 123000 ± 1000 and branch 2 if values are 120000 ± 1000!

P.S. I’ve been having trouble accessing the values from the data trees and manipulating, and I haven’t found the best source for related literature yet. The grasshopper primer has very limited information. If someone could point me to a better resource (if you can think of one), that would be super helpful.

Thanks!

Branching

Like so? number-tree-sort.gh (26.3 KB)

Thank you for your prompt response @DavidRutten. That’s perfect!

Hi again @DavidRutten, I was trying to modify the code to expand it for branched lists with another added branch by adding a loop in the definition as follows:

’ Iterate over all values.
For Each number As Double In N
’ Iterate over target ranges.
For j As Int32 = 0 To N.BranchCount - 1
For i As Int32 = 0 To ranges.Length - 1
If (ranges(i).IncludesParameter(number)) Then
’ Found a range which contains the number.
tree.Add(number, New GH_Path(j, i))
Exit For
End If
Next
Next
Next

But it gives me an error saying “Error (BC32023): Expression is of type ‘Grasshopper.DataTree(Of Double)’, which is not a collection type. (line 99)”.

I went through a similar thread here but didn’t know what the resolutions was.

These two loops conflict logically:

For Each number As Double In N
For j As Int32 = 0 To N.BranchCount - 1

The former iterates over all values in the tree (which I think is already illegal, you cannot just iterate bluntly over all items in a tree, you should iterate over all branches, then iterate over all items in each branch), the latter iterates over all branches, and does so for each item. This just doesn’t make any sense.

Here’s the loops you need:

    For k As Int32 = 0 To N.BranchCount - 1
      Dim branch As List(Of Double) = N.Branch(k)
      For j As Int32 = 0 To branch.Count - 1
        For i As Int32 = 0 To ranges.Length - 1
          If (ranges(i).IncludesParameter(branch(j))) Then
            tree.Add(branch(j), New GH_Path(j, i))
            Exit For
          End If
        Next
      Next
    Next