quick on arrays

For general discussion related FlowStone
Post Reply
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

quick on arrays

Post 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.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: quick on arrays

Post by Nubeat7 »

its pretty easy in ruby too, here are some combinings..
Attachments
arraycombinings .fsm
(1.53 KiB) Downloaded 974 times
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: quick on arrays

Post 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? :ugeek:
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: quick on arrays

Post by RJHollins »

Yes ... Thanks NuBeat !!!
8-)
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: quick on arrays

Post 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). :-)
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: quick on arrays

Post 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...

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!
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: quick on arrays

Post 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 :)
Post Reply