comparing 2 arrays (ruby)

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

comparing 2 arrays (ruby)

Post by tester »

I need to do one of two things:

a) compare 2 arrays of elements and get an output with 0's (element at index not equal) and 1's (element at index equal)
or
b) determine which elements on input array changed after single retrig, and get an output like described above.

I guess the (a) is easier to implement.
It would be like this:

array1:

11
9
3
40
5

array2 (= array1 after change)

11
25
34
40
7

output:

1
0
0
1
0

(no sorting, no duplicate removing). How to make it with ruby?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing 2 arrays (ruby)

Post by tester »

I think I found something, but... How to push these values to output?

I mean - ruby window shows correct answers, but when I add "output..." then it outputs only 0's or nothing.
Attachments
compare-ary.fsm
(384 Bytes) Downloaded 901 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing 2 arrays (ruby)

Post by tester »

...not what I expected. Right now I can get this - works only with output changed to "string". But how to get simple array of 0/1 integers instead?
Attachments
compare-ary1.fsm
(425 Bytes) Downloaded 891 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
TrojakEW
Posts: 111
Joined: Sat Dec 25, 2010 10:12 am
Location: Slovakia

Re: comparing 2 arrays (ruby)

Post by TrojakEW »

Now just simply convert them.
Attachments
compare-ary1.fsm
(465 Bytes) Downloaded 929 times
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: comparing 2 arrays (ruby)

Post by Nubeat7 »

it is possible with just one map command too
for comparing things with 2 options the ternary operator ( ? : ) always comes very handy

it works like this:
(condition) ? (when true) : (when false)

Code: Select all

output @in1.map.with_index { |x,i| x = x==@in2[i] ? 1 : 0 }
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing 2 arrays (ruby)

Post by tester »

Thanks!
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing 2 arrays (ruby)

Post by tester »

So this little schematic will detect which values in input array have changed (per cycle).
Attachments
detect-valchange.fsm
(743 Bytes) Downloaded 942 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Post Reply