Page 3 of 5

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 1:05 pm
by billv
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...
ScreenShot305.png
ScreenShot305.png (18.01 KiB) Viewed 15846 times

Hope this helps ...

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 1:13 pm
by trogluddite
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'.

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 1:14 pm
by tester
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.

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.

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 1:17 pm
by nix
aother way to get a 2d array with prims is to use 'mem array'
thought I'd mention it as troggie brought it up

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 1:25 pm
by tester
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?

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 1:39 pm
by trogluddite
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

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 2:17 pm
by billv
thanks trog..i did get it running your way...
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...

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 2:56 pm
by tester
Okay, thanks, will try it tonight.
Maybe I am able on my own to combine these three things into one functional... THING. :ugeek:

What was the rule on machines vs humans? Simple things require complex operations? :mrgreen:

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 9:08 pm
by tester
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?

Re: two things array/ruby related

Posted: Fri Nov 01, 2013 10:04 pm
by Nubeat7
yes you could make an array class methode out of it like this:

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