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
7 posts
• Page 1 of 1
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.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: quick on arrays
its pretty easy in ruby too, here are some combinings..
- Attachments
-
- arraycombinings .fsm
- (1.53 KiB) Downloaded 833 times
-
Nubeat7 - Posts: 1347
- Joined: Sat Apr 14, 2012 9:59 am
- Location: Vienna
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.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
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.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: quick on arrays
tester wrote:...get common values (from two arrays) within a range?
You'd do that in two steps - first find the common elements, as in Nubeat's example...
- Code: Select all
common = array_a & array_b
Then use the "keep_if" or "delete_if" methods to find the items meeting the condition - for example if you only want numbers less than ten, and only even numbers...
- Code: Select all
common.keep_if{|item| item < 10 && item.even?}
The syntax does take a little getting used to, but is very powerful...
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?
end
Inside this, the first thing is to name a variable in between the '|' characters - it's just a dummy variable that takes the value of each item of the array in turn. The code part that follows is made like any true/false boolean test, using the variable you just declared (in this case 'item', but the name can be anything).
If 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!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
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_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
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 80 guests