tester wrote:I don't get that one. Could you upload it as a module with text connected on input?
What output would you need - e.g. String Array, Number Array, special "a/b/c" string format ?
billv wrote:I want to basicly...
my_array = [@a1, @a2, @a3]
If your '@a' variables are all arrays, then declaring 'my_array' like that is all that you need to do. The splitting part in the example is only there because tester is using a custom string format for data storage.
Nesting arrays like this is the standard way to make 2-dimensional (or more) arrays in Ruby...
Code: Select all
row0 = [1,2,3]
row1 = [4,5,6]
row2 = [7,8,9]
# Create a nested 2-dimensional array
array_2D = [row0, row1, row2]
# Access a given row
row_index = 1
row = array_2D[row_index] #=> [4,5,6]
# Access a specific cell
row_index = 0
column_index = 2
cell = array_2D[row_index][column_index] #=> 3
# Access a specific column
column_index = 1
column = array_2D.transpose[column_index] #=> [2,5,8]
Note that you can't get a single cell using
array_2D[row,column], you must have the two separate pairs of brackets
array_2D[row][column].
If you use Ruby value connectors (white 'V'), you can send a nested array between Ruby prim's intact - but green arrays can only be one-dimensional.
tester wrote:One thing regarding "get column" module. How to fill the array with nils, so that it will not produce an error, if some rows have less values?
billv wrote:If arrays are not the same size exception is raised....
This little method can be used to fix an array size at a given length...
Code: Select all
def fix_array_size(array, length, pad=nil)
size = array.size
case
when size > length
array[0...length]
when size < length
array + Array.new(length-size,pad)
else
array.dup
end
end # fix_array_size method ends here
# EXAMPLE USAGE
test_array = [1,2,3,4]
fix_array_size(test_array,3) #=> [1,2,3]
fix_array_size(test_array,6) #=> [1,2,3,4,nil,nil]
fix_array_size(test_array,6,0) #=> [1,2,3,4,0,0]
The method definition (from "def" up to the non-indented "end") just goes at the start of your code. You then call the method with either two or three parameters...
test_array.fix_array_size(array, size) #=> Increased length is filled with 'nil'
test_array.fix_array_size(array, size, pad) #=> increased length is filled with 'pad' object.
It always returns a new array, even if the length isn't changed, so you can always use it safely without affecting the original array.
To use with "tester style" separator string, you'd need to 'split' the string, call the fix_array_size method on the resulting array, then recombine with 'join'....
Code: Select all
test = "a/b"
array = test.split('/')
fixed = fix_array_size(array, 4, "") #NB pad with empty strings
new_test = fixed.join('/') #=> "a/b//"
tester wrote:@variables for outputs
There are '@' variables for outputs, but they have a strange behaviour, so I tend to avoid them.
- Setting the value of an '@' output doesn't send the value, you must use the "output" command otherwise the connector won't be updated.
- Reading the value only works after there has been an "output" command - prior to this, they will always return 'nil'.