@pixero said:
Ok, that make sence.
Following Jims example would it be something like:
att = 0
> ary.each { |item|
> item.set_attribute("Dict Name", att)
> att+=1
> }
1) There's a parameter missing in set_attribute, the 3rd arg is the value argument to assign to the attribute. (Would the example assign nil, or would an ArgumentError be raised?)
2) The examples will begin at 0, not 1. (You could adjust the start ordinal for var att in it's assignment statement.)
3) Be sure the array is sorted first the way you wish to have the attributes numbered. You may need to write your own sort method if the objects in the array are complex objects, such as Drawingelement subclasses. Meaning you may wish to sort by some property of the object, such as it's height above the XY plane (just as an example.)
4) Whenever you need to iterate an enumerble Ruby object, you can use the each_with_index method (that is inherited from the "mixin" module Enumerable.)
Here I add 1 to the index, so the numbering starts at 1:
dict = "My Dictionary Name"
val = "some value"
ary.each_with_index { |item,pos|
item.set_attribute( dict, pos+1, val )
}
If you still wish to use 0 padded string attribute names:
dict = "My Dictionary Name"
val = "some value"
places = 6
ary.each_with_index { |item,pos|
item.set_attribute( dict, (pos+1).to_s.rjust(places,'0'), val )
}
Note: The 2nd arg in rjust, the padstring, is optional and defaults to a space character.