Grasshopper

algorithmic modeling for Rhino

Hello!
This is my very first attempt to produce some script.

I would like to create a vb component, that, for now, reads a list of integers, and produces a list of the same integers, but deleting from that every number already occurred.

But I ran into some problems

Here is the screenshot of the first attempt:and here the script:

Dim voxel_out As New list (Of Integer)
Dim voxel_in As New list (Of Integer)
Dim voxel_in_service As New list (Of Integer)
Dim a As Integer
Dim i As Integer
Dim x As Integer
voxel_in = VoxelListIN
For x = 0 To voxel_in.count() - 1
  a = voxel_in(0)
  voxel_out.add(a)
  'deletes form voxel_in all the repetitions of a:
  For i = 1 To voxel_in.count() - 1
    If voxel_in(i) <> a Then
    voxel_in_service.add(voxel_in(i))
  End If
  Next
  voxel_in = voxel_in_service
  voxel_in_service.clear()
  Next
VoxelListOut = Voxel_out

the problem with this script is that it says, as an error, that "the index is not included in the interval. It is requested a value which is not negative and smaller than the dimension of the set". The same kind of error I get when I screw up with the indices of the FOR command.
The strange thing is that if I remove the

 voxel_in_service.clear()

line, the script runs without errors (but of course, with a wrong result).

Therefore I thought it was because the .clear method was not fully supported, so I wrote the following:

Dim voxel_out As New list (Of Integer)
Dim voxel_in As New list (Of Integer)
Dim voxel_in_service As New list (Of Integer)
Dim reset_list As New List (Of Integer)
Dim a As Integer
Dim i As Integer
Dim x As Integer
voxel_in = VoxelListIN
For x = 0 To voxel_in.count() - 1
  a = voxel_in(0)
  voxel_out.add(a)
  'deletes form voxel_in all the repetitions of a:
  For i = 1 To voxel_in.count() - 1
    If voxel_in(i) <> a Then
    voxel_in_service.add(voxel_in(i))
  End If
  Next
  voxel_in = voxel_in_service
  voxel_in_service = reset_list
  Next
VoxelListOut = Voxel_out
reset = reset_list

but, very surprisingly to me, the line

voxel_in_service = reset_list

instead of setting back voxel_in_service to it's original state, sets, as you can see from the "reset" output, reset_list to the values of Voxel_in_service. Why is it doing that?!
Thank you very much!

Views: 1372

Replies to This Discussion

The set component does this.

I know there are some simpler ways to do that, but I decided to script it for learning how to script and also because this is just a part of what I want to achieve with the final script...

but thank you anyway for your reply!

Hi Matteo,
First get clear about the difference between value types (e.g. Integer) and reference types (e.g. List).

You have written:

voxel_in = voxel_in_service
voxel_in_service.clear()

After the first line both voxel_in and voxel_in_services are referring to the same list. If you clear the list stored in voxel_in_service then you also clear the list stored in voxel_in because it is the same. Both variables are now referring to an empty list!

You have to do something like that:

voxel_in.Clear()
voxel_in.AddRange(voxel_in_service)
voxel_in_service.Clear()

The next problem is that your For loop gets from 0 to voxel_in.count() - 1. But the length of voxel_in is changing! Use a While loop instead:

While (voxel_in.Count > 0)
...
End While

After these changes your code should work.

A shorter way to remove duplicates:

If (list.Count = 0) Then Return

list.Sort()

Dim i = 1

While (i < list.Count)
  If (list(i - 1) = list(i)) Then
    list.RemoveAt(i)
  End If
  i += 1
End While

Or use the build-in method:

list = list.Distinct().ToList()

but keep in mind that this method creates a new list!

Greetings!

thank you very much!

I'm really new to programming and all this concepts are still a bit confused in my mind, though I feel I'm improving! :)

Greetings, to you as well!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service