Help with arrays
-
Your code is almost there.
There is the .each_with_index method that may be more readable.
test_array.each_with_index {|e,i|
A question... does the data array contain beforehand, the same number of elements, as the new_array.
If not, you need to use a hash with integer keys, instead of an array as the ouptut data.
-
Jan, what platform are you on, PC or Mac?
-
PC.
Thanks for your answer. Is my way of putting the element from array into new_array correct?
What happens if a index is occupied? Is that element moved to the next index or simply overwritten?
(new_array is empty, you could say I sort array into new_array.) -
The way I'd do it is probably not the proper way... but it'd work...
` tarray=[11, 22,33, 44]
aarray=[1, 22, 333, 44]
narray=Array.new(tarray.length)
p narray[nil, nil, nil, nil]
tarray.length.times{|i| narray[i]=tarray[i] if tarray[i]==aarray[i] }
p narray
aarray=[nil, 22, nil, 44]`This works if you want to match exact array entries BUT use '
array.include?(value)
' if you simply want to find the entry anywhere in the arrays -
@pixero said:
Is my way of putting the element from array into new_array correct?
No not with .each, but possibly using .each_with_index
@pixero said:
What happens if a index is occupied? Is that element moved to the next index or simply overwritten?
It is always overwritten when you use the [=] assigment method, and the index is occupied.
@pixero said:
(new_array is empty, you could say I sort array into new_array.)
The [=] assigment method will automatically insert nil (blank,) index members if you specify an index greater than those in the array.
If the array is empty, and you do a[3='Jan'], the array will be [nil,nil,nil,'Jan']
if the array was ['Bill','Dan'] and you do a[4='Jan'], the array will be ['Bill','Dan',nil,nil,'Jan'] -
@pixero said:
..., you could say I sort array into new_array.
IF you REALLY want to sort to a new array, use new_array = old_array.sort
IF you wish to sort an array itself, use old_array.sort!
(notice the exclamation point.) -
@pixero said:
PC.
Do you have the Ruby Core Reference CHM (Windows Compiled Help HTML,) for Ruby 1.8.6 ??
If not download it from my post in the Ruby Resources topicNotice at the top of each class or module, the Mixin Modules are listed.
Array class has Enumerable mixed-in, so it inherits all those methods.
-
If you could tell us what kind of test your doing.. it would help us choose the proper Enumerable method for you.
I'm thinking probably the .collect method is perhaps what you want. -
Its actually the continuation of this thread: http://forums.sketchucation.com/viewtopic.php?f=180&t=33862
I have to digest this and try what you recommended before getting back.
-
@pixero said:
Its actually the continuation of this thread: [url]=ttp://forums.sketchucation.com/viewtopic.php?f=180&t=33862]How to compare points on a plane?[/url]
OK.
So the data_array contains Geom::Point3d objects?
What kind of object does the test_array contain?What is the logical condition that you want the data_array elements to be added to the new_array?
If a false results from the logical test, is putting nil into the new_array OK, or do you wish some other value (perhap [0,0,0] or whatever) ?We need to be careful when comparing Geom::Point3d objects, as Google overrode the .< and .== methods (to make them dependant upon the 0.001" internal tolerance.) You should decide if that's OK for your purposes.
Advertisement