Page 1 of 5
two things array/ruby related
Posted: Thu Oct 31, 2013 10:19 am
by tester
Somewhat I'm overwhelmed, so sorry if some question repeats.
I'm looking for simple (starting to like these single-line codes) ruby solutions to two things.
1. Get column.
Let say that you have an array (x rows, y columns). I'd like to get specific y column, according to separator. Like this.
a/b/c
1/2/3
q/1/m
column 2:
b
2
1
I know there were SM modules for that, but they are appropriately slower than ruby.
Actually it could be like "get multiple columns", similar to what is in p2 in regards to rows.
2. Get multiple rows according to index list.
Let say that I have one array with multiple rows, and second array with list of indexes. On output I'd like to have only these rows, which are indexed in second array. Like this.
array1:
line1
line2
line3
line4
line5
array2:
0
2
output:
line1
line3
---------
I need to redesign some parts of current project, due to green based slowdowns.
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 2:34 pm
by Nubeat7
dont know exactly how you need it but with array.indexes(idx1,idx2,idx3,...) you could call specific indexes from an array..
depending on your needs i would also have a look at hashtables.. in ruby
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 3:34 pm
by tester
It is totally non-audio.
In typical scenario, it's related to external custom lists (text files), made user friendly. Like this:
item1a/item1b/item1c/item1d
item2a/item2b/item2c/item2d
item3a/item3b/item3c/item3d
...
itemNa/itemNb/itemNc/...
where "item" is a whatever word or whatever number; in general, these can be labels associated with other labels or with numerical values, and pretty often - used with randomized selection. Custom lists don't have any headers, they are just lists of items.
(I'm not using comma, because it is interpreted by some FS string prims as well with end line marks).
One thing I'm using pretty often, is - getting specific column of items. In simple short lists, old SM solution works fine (as far I remember, it was named "stride"), but here - I need to speed up the process, thus - using ruby.
Another thing I'm starting to use, is to get selected multiple rows (just by following list of indexes), for example row1 and row3 from the list. Thus - only these items are then in use.
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 4:46 pm
by CoreStylerz
Explain better what u need.
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 5:29 pm
by tester
There is no way to explain it simpler.

I need exactly that. In simplest form of ruby.
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 6:31 pm
by Nubeat7
maybe some .join and .select combinations to create an array or more arrays form the string and then select the item per index ( i`m not at home to build something for you ) but why not using 3 arrays instead? also a user input format like this as text file will have a big chance of user input errors which you should take care of... i still would recommend hashtables with some secure input mask to prevent userinput errors..
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 7:36 pm
by tester
What I pointed in above posts - were only examples on the topics itself. It's not that "there are only 3 columns", or that these items have fixed lenghts. It's the idea. The items as strings - are multi-word texts combined numbers (depending on where such solutions are used). Most data is reloaded from external text files, freely edited (and sorted) by user; that why I don't want to add any headers.
These things are relatively easy to get with green prims, but greenery gets slow, when dealing with dozens of iterations at each click. Difference between ruby and greenery is like between fraction of second and few seconds, so it is an issue.
As I said - this is not audio related. It's processing of textual data in various, multiple ways, and dealing with internally associated, numerical data.
I suspect, that first thing (getting the individual column) could be done by using "rotate array" (rotation according to user separator, like "/" - still not sure to make it). Thus - each column would become a row, and rows are indexed automatically in FS by "new line" separator.
As for second part (getting multiple lines according to list of indexes) - I suspect, that there is very easy way to get on output only these lines, that match the indexes on the second list. I just have no idea how to make it.
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 9:00 pm
by trogluddite
To use a second array to look up a list of indexes, you can use a special form of "values_at"...
Code: Select all
array1 = ['a', 'b', 'c' ,'d', 'e', 'f']
array2 = [1,3,4]
result = array1.values_at(*array2) #=> ['b', 'd', 'e']
The * in front of array2 is important - you would normally give 'values_at' a list of indexes as the arguments, e.g.
array1.values_at(1,3,4), so an array normally wouldn't work as it isn't an Integer. The * is a special Ruby syntax that 'flattens' an array into a list, so that it gets seen as a sequence of individual parameter values.
(NB - not just for 'values_at', it can be used in other situations like this).
tester wrote:I suspect, that first thing (getting the individual column) could be done by using "rotate array"
You're on the right track - but you need an array where each row is itself an array - i.e. nested array structure. So the string for each row will need converting to array form...
Code: Select all
my_array = ["1/2/3", "a/b/c", "X/Y/Z"]
# Convert rows into sub-arrays
nested_array = my_array.map do |row|
row.split('/')
end
# You now have a nested array...
# [ ["1","2","3"], ["a","b","c"], ["X","Y","Z"] ]
# Now transpose so that columns become rows and vice versa
transposed_array = nested_array.transpose
# transposed array will now be
# [ ["1","a","X"], ["2","b","Y"], ["3","c","Z"] ]
# Get a column
column = transposed_array[0] #=> ["1", "a", "X"}
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 9:20 pm
by Nubeat7
Nubeat7 wrote:dont know exactly how you need it but with array.indexes(idx1,idx2,idx3,...) you could call specific indexes from an array..
depending on your needs i would also have a look at hashtables.. in ruby
my suggestion of using .indexes is a little out of date

it is called .values_at , thanks trog, just had a quick search on google - always better to watch at the actual api doc before posting some outdated stuff without testing
and wow i like this asterisk more in ruby than in c++
Re: two things array/ruby related
Posted: Thu Oct 31, 2013 9:23 pm
by RJHollins
not to distract from the OP ... and thanks for the examples TROG ...
but I've been trying to figure out how to change the output of the RUBY box from the proper ARRAY display:
["1", "a", "X"]
to one that is 'clean' .... 1 a X
or
1
a
X
I'm thinking the array is extracted into a string format. But I more familliar using PRIMS and still easily confused with RUBY. [need more practise

]
Thanks for info!