Page 1 of 1
quick on arrays
Posted: Tue Oct 15, 2013 10:07 am
by tester
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.
Re: quick on arrays
Posted: Tue Oct 15, 2013 11:39 am
by Nubeat7
its pretty easy in ruby too, here are some combinings..
Re: quick on arrays
Posted: Tue Oct 15, 2013 1:46 pm
by tester
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?

Re: quick on arrays
Posted: Tue Oct 15, 2013 5:22 pm
by RJHollins
Yes ... Thanks NuBeat !!!

Re: quick on arrays
Posted: Tue Oct 15, 2013 6:00 pm
by tester
...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).

Re: quick on arrays
Posted: Tue Oct 15, 2013 7:27 pm
by trogluddite
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...
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.
Re: quick on arrays
Posted: Tue Oct 15, 2013 11:19 pm
by billv
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
