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
quick on arrays
quick on arrays
What is the simplest ruby formula for (unsorted; examples show sorted) arrays to:
1) Remove smaller array from larger one:
Let say that "main" array is made of numbers 1-10 (yep, 1,2,3,4,5,6,7,8,9,10).
Let say that "feed" array is made of elements: 1,4,7,10
Resulting array should be "main" minus "feed": 2,3,5,6,8,9
2) Pick common part from two arrays:
Let say that array 1 is made of: a,b,c,1,2,3
Let say that array 2 is made of: a,b,c,4,5,6
Resulting array should be: a,b,c
*
Doing these in greens is easy and possible, but resource consuming.
1) Remove smaller array from larger one:
Let say that "main" array is made of numbers 1-10 (yep, 1,2,3,4,5,6,7,8,9,10).
Let say that "feed" array is made of elements: 1,4,7,10
Resulting array should be "main" minus "feed": 2,3,5,6,8,9
2) Pick common part from two arrays:
Let say that array 1 is made of: a,b,c,1,2,3
Let say that array 2 is made of: a,b,c,4,5,6
Resulting array should be: a,b,c
*
Doing these in greens is easy and possible, but resource consuming.
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.
Re: quick on arrays
its pretty easy in ruby too, here are some combinings..
- Attachments
-
- arraycombinings .fsm
- (1.53 KiB) Downloaded 988 times
Re: quick on arrays
Thanks for examples Nubeat7.
That sort of things, in various areas of use - are appreciated.
What else do you know that could be done with arrays?
That sort of things, in various areas of use - are appreciated.
What else do you know that could be done with arrays?
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.
Re: quick on arrays
Yes ... Thanks NuBeat !!!

Re: quick on arrays
...get common values (from two arrays) within a range?
Like:
1,2,3,4,5,6,7,8,9
1,5,6,9
min: 2 (or >2)
max: 8 (or <8)
result: 5,6
...cross-array operations between array elements?
I need to get back to my old project, to recall what was it about (and why it made me a headache).
Like:
1,2,3,4,5,6,7,8,9
1,5,6,9
min: 2 (or >2)
max: 8 (or <8)
result: 5,6
...cross-array operations between array elements?
I need to get back to my old project, to recall what was it about (and why it made me a headache).
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.
- trogluddite
- Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: quick on arrays
You'd do that in two steps - first find the common elements, as in Nubeat's example...tester wrote:...get common values (from two arrays) within a range?
Code: Select all
common = array_a & array_bCode: Select all
common.keep_if{|item| item < 10 && item.even?}The test to use for keeping (or deleting) items is defined by putting it inside curly brackets - OR, for a bigger chunk of code, between "do" and "end"...
Code: Select all
common.keep_if do |item|
item < 10 && item.even?
endIf the boolean result for a value is true. it stays in the array, otherwise it is discarded (or vice versa for "delete_if").
Note that, with these methods, you don't have to assign the result to a new variable, the initial array is directly modified.
I'll be the first to admit, these little code "blocks" that you can 'feed' to methods were one of the trickiest things for me to understand when starting with Ruby - but a great many methods can use them, and they are one of Ruby's most powerful features.
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!
Re: quick on arrays
Thanks guys....
Was adding heaps of functions to the ruby array thing i made to keep learning,
and build a mega-array module that sort of 'does it all'..
Adding that stuff in.....
has about 20 functions already..
added auto array gen from sm too...
Will upload when done....
Cheers
Was adding heaps of functions to the ruby array thing i made to keep learning,
and build a mega-array module that sort of 'does it all'..
Adding that stuff in.....
has about 20 functions already..
added auto array gen from sm too...
Will upload when done....
Cheers
BV MUSIC SYDNEY AUSTRALIA..Songwriting and Software development
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_b ... R8R7K8GZ4L
Headquartershttps://www.bvmusicsydneyaustralia.com/
Spotifyhttps://open.spotify.com/artist/7JO8QM40mVmHb7pAwKPJi0
Donatationhttps://www.paypal.com/donate/?hosted_b ... R8R7K8GZ4L