If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
two things array/ruby related
Re: two things array/ruby related
That's where i started after my last post....tester wrote:How to fill the array with nils,
It's only way to make transpose method work..
Got it working...but its not pretty...
Code: Select all
def init
@a = [0]
@b = [0]
@c = [0]
end
if @a ==[] then @a =[0] end
if @b ==[] then @b =[0] end
if @c ==[] then @c =[0] end
def event i,v
if i == 0 or 1 or 2 or 3 or 4 then
findlength =Array.new
a1max = @a.each_index.max
a2max = @b.each_index.max
a3max = @c.each_index.max
a = @a.length
b = @b.length
x = @c.length
findlength =[a,b,x]
c = findlength.each_with_index.max
d = findlength.each_with_index.min
if c[0] != d[0] then
@b.fill("nil",a2max+1..a1max)
@a.fill("nil",a1max+1..a2max)
@c.fill("nil",a1max+1..a2max)
e = Array.new([@a,@b,@c])
f = e.transpose
column = f[@index]
@q = column
output 0, @b
output 1, @a
output 2, @q
end
watch @q
end
endHeadquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_b ... R8R7K8GZ4L
- trogluddite
- Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: two things array/ruby related
What output would you need - e.g. String Array, Number Array, special "a/b/c" string format ?tester wrote:I don't get that one. Could you upload it as a module with text connected on input?
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.billv wrote:I want to basicly...
my_array = [@a1, @a2, @a3]
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]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?
This little method can be used to fix an array size at a given length...billv wrote:If arrays are not the same size exception is raised....
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]
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//"There are '@' variables for outputs, but they have a strange behaviour, so I tend to avoid them.tester wrote:@variables for outputs
- 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'.
Don't stagnate, mutate to create!
Re: two things array/ruby related
//edit: Trog was faster.
Trog - I managed to compile that thing from your notes in my next post.trogluddite wrote:What output would you need - e.g. String Array, Number Array, special "a/b/c" string format ?tester wrote:I don't get that one. Could you upload it as a module with text connected on input?
Feel free to donate. Thank you for your contribution.
Re: two things array/ruby related
thought I'd mention it as troggie brought it up
Re: two things array/ruby related
Thanks. But there is one more thing. The "fixed lenght" (amount of items per line) is "defined freely" by user's interaction with an external text file; i.e. user just adds something to external text file, to any line, like this:trogluddite wrote: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//"
...
...phrase1/phrase2/["okay, lets add... phrase3 here?"]
...
So there would have to be something, that measures which line (row) is the longest one (in terms of separated items), and this value would be used as the "fixed lenght". How to make that?
Feel free to donate. Thank you for your contribution.
- trogluddite
- Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: two things array/ruby related
Oddly, there isn't a basic "set_length" method in the Ruby API. Trimming shorter is easy, but extending always needs multiple operations - especially if you need the "padding" to contain a specific value.tester wrote:I'm pretty sure, there must be easier way.
tester wrote:something, that measures which line (row) is the longest one (in terms of separated items)
Code: Select all
test = ["a/b/c","c/d","e/f/g/h"]
# Get array of row lengths by counting separators
lengths = test.map{|row| row.count('/')+1} #=> [3,2,4]
# Get the longest one
longest = lengths.max #=> 4Don't stagnate, mutate to create!
Re: two things array/ruby related
I just need to get rid of this '6' ,
Code: Select all
a = fix_array_size(@a,6)Will try tommorrow...
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_b ... R8R7K8GZ4L
Re: two things array/ruby related
Maybe I am able on my own to combine these three things into one functional... THING.
What was the rule on machines vs humans? Simple things require complex operations?
Feel free to donate. Thank you for your contribution.
Re: two things array/ruby related
I'm getting error on "fix_array_size" part (it says here: NoMethodError - undefined method)trogluddite wrote: 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//"
...or should I paste first this?
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
endFeel free to donate. Thank you for your contribution.
Re: two things array/ruby related
Code: Select all
class Array
def fix_array_size( length, pad=nil)
size = self.size
case
when size > length
self[0...length]
when size < length
self + Array.new(length-size,pad)
else
self.dup
end
end
endthen you can use this methode like this in every other ruby module:
Code: Select all
test = "a/b"
array = test.split('/')
fixed = array.fix_array_size( 4, "") #NB pad with empty strings
new_test = fixed.join('/') #=> "a/b//"