Uniq! not working ?
-
@remus said:
with regards to using sets, its only a small array (max 20-30 elements) so it shouldnt matter.
Then Arrays might be faster.
-
You can also use Array.concat to append new arrays as elements instead of a single array.
a = [] a.push([1, 2, 3]) ==> [[1, 2, 3]] a = [] a.concat([1, 2, 3]) ==> [1, 2, 3]
-
@remus said:
face.edges.each{|e| faces.push(e.faces)}
The correct code should be something like:
face.edges.each { |e| faces += e.faces }
For small numbers of elements, uniq! should be OK. Otherwise, you might consider using a Hash or a Set to avoid redundant elements.
Fredo
-
Fredo, it gives me a syntax error when i try that, bear in mind faces is an array (and i dont think += is defined for arrays.)
-
Excuse me for stepping in here.
Out of curiosity, are there a lot of terms like "uniq" in the ruby language? Couldn't this get confusing? Or can you use interchangeably "unique"? -
I dont think you can use "unique" interchangeably but its not too confusing really, if anything i find the differences help you remember the methods.
-
Thanks, Remus.
Again, please excuse the intrusion. -
SUp's own
.make_unique
method only applies to groups and instances of component definitions.
Its.unique_name
methods applies to definitions and layers name creation.
Ruby's own.uniq
method returns an array (etc) of unique items ('cloned'), missing out duplicates,,,
array1=[1,1,2,3]; array2=array1.uniq >>> array2 returns [1,2,3] but array1 still returns [1,1,2,3]
whereas using.uniq!
changes the original array to a unique one...
array1=[1,1,2,3]; array1.uniq! >>> array1 now returns [1,2,3]All very useful in there own ways...
-
@remus said:
...to try and create an array of the faces connected to a face...
EdgeUse.partners
http://code.google.com/apis/sketchup/docs/ourdoc/edgeuse.html#partners
@unknownuser said:The partners method is used to retrieve all of the partner edge uses. This method allows you to find all Faces that use an edge.
EDIT: Never-mind. I think the flatten! array means by ThomThom is much simpler. Compact code, let ruby do the hard work.
-
@mitcorb said:
Excuse me for stepping in here.
Out of curiosity, are there a lot of terms like "uniq" in the ruby language? Couldn't this get confusing?Many of the method names came from Unix and Perl, so they are familiar to people with that background. Well there are some 'shortened nicknames', such as: assoc, eql, attr, dup, eval, stat, sync, const, undef,... etc. sprinkled throughout ruby modules, but No not a majority. A very small minority in fact. But you get used to them.
@mitcorb said:
Or can you use interchangeably "unique"?
You could (if you felt more comfortable,) create singleton methods that provide an alias.
arr = [1,2,1,3,4] def arr.unique! self.uniq! end arr.unique! >> [1,2,3,4] arr2=[5,6,7,5] arr2.unique! >> Error; #<NoMethodError; undefined method `unique!' for [5, 6, 7, 5];Array>
So it only works for the single instance 'arr'.
This avoids globally changing Base classes, which most people get upset about. -
@remus said:
Fredo, it gives me a syntax error when i try that, bear in mind faces is an array (and I dont think += is defined for arrays.)
It must be something else (do you have a precise description of the error).
+
is defined for Arrays (it's equivalent toconcat
), so+=
is meaningful (I use it frequently).Fredo
-
@unknownuser said:
@remus said:
Fredo, it gives me a syntax error when i try that, bear in mind faces is an array (and I dont think += is defined for arrays.)
It must be something else (do you have a precise description of the error).
+
is defined for Arrays (it's equivalent toconcat
), so+=
is meaningful (I use it frequently).Fredo
I get a syntax error as well:
[1,2,3] += [4,5] Error; #<SyntaxError; (eval);894; compile error (eval);894; syntax error [1,2,3] += [4,5] ^> (eval);894
Is it not that you have some code that has added this as extra functionality?
-
hm... when I defined the variables
face
andfaces
- and ran the code - it worked....
face.edges.each { |e| faces += e.faces }
now I'm confused
-
Tom,
It only works on variables, because you actually modify the contnet of the left-side array (and I guess [3,5] is considered as a constant).
I tried in the console:
a = [1,2] [1, 2] b = [3, 4] [3, 4] a += b [1, 2, 3, 4] [1,2] += b Error; #<SyntaxError; (eval);130; compile error (eval);130; syntax error [1,2] += b ^> (eval);130
Fred
-
Working now Must have missed something last time i tried it.
Advertisement