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
43 posts
• Page 3 of 5 • 1, 2, 3, 4, 5
Re: two things array/ruby related
tester wrote:How to fill the array with nils,
That's where i started after my last post....
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
end
really seems like too much work using transpose...so I'm cheating with this...
Hope this helps ...
BV MUSIC SYDNEY AUSTRALIA..Songwriting and Software development
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_button_id=HEUR8R7K8GZ4L
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_button_id=HEUR8R7K8GZ4L
- billv
- Posts: 1157
- Joined: Tue Aug 31, 2010 3:34 pm
- Location: Australia
Re: two things array/ruby related
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'.
Last edited by trogluddite on Fri Nov 01, 2013 1:19 pm, edited 1 time in total.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: two things array/ruby related
I'm pretty sure, there must be easier way. This seems to be one of rather basic operations, thus - probably there is something specific designed for it.
//edit: Trog was faster.
Trog - I managed to compile that thing from your notes in my next post.
//edit: Trog was faster.
trogluddite wrote: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 ?
Trog - I managed to compile that thing from your notes in my next post.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: two things array/ruby related
aother way to get a 2d array with prims is to use 'mem array'
thought I'd mention it as troggie brought it up
thought I'd mention it as troggie brought it up
-
nix - Posts: 817
- Joined: Tue Jul 13, 2010 10:51 am
Re: two things array/ruby related
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//"
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:
...
...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?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: two things array/ruby related
tester wrote:I'm pretty sure, there must be easier way.
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: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 #=> 4
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: two things array/ruby related
thanks trog..i did get it running your way...
I just need to get rid of this '6' ,
so it runs auto..like i had in prev post...
Will try tommorrow...
I just need to get rid of this '6' ,
- Code: Select all
a = fix_array_size(@a,6)
so it runs auto..like i had in prev post...
Will try tommorrow...
BV MUSIC SYDNEY AUSTRALIA..Songwriting and Software development
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_button_id=HEUR8R7K8GZ4L
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_button_id=HEUR8R7K8GZ4L
- billv
- Posts: 1157
- Joined: Tue Aug 31, 2010 3:34 pm
- Location: Australia
Re: two things array/ruby related
Okay, thanks, will try it tonight.
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?
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?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: two things array/ruby related
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//"
I'm getting error on "fix_array_size" part (it says here: NoMethodError - undefined method)
...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
end
It looks so. Can this second part be used in separate ruby window and sent via wireless connector to all ruby modules?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: two things array/ruby related
yes you could make an array class methode out of it like this:
you can put this array class methode in a module somewhere in your schematic, but it is important that you do this before adding any other ruby module inside!
then you can use this methode like this in every other ruby module:
ps:if you use my arrayclass methode expansions module you also can add the methode there
- 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
end
you can put this array class methode in a module somewhere in your schematic, but it is important that you do this before adding any other ruby module inside!
then 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//"
ps:if you use my arrayclass methode expansions module you also can add the methode there
-
Nubeat7 - Posts: 1347
- Joined: Sat Apr 14, 2012 9:59 am
- Location: Vienna
43 posts
• Page 3 of 5 • 1, 2, 3, 4, 5
Who is online
Users browsing this forum: No registered users and 70 guests