Support

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

comparing 2 arrays (ruby)

For general discussion related FlowStone

comparing 2 arrays (ruby)

Postby tester » Fri Jan 03, 2014 12:14 am

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)

Postby tester » Fri Jan 03, 2014 12:41 am

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 794 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)

Postby tester » Fri Jan 03, 2014 1:20 am

...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 790 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)

Postby TrojakEW » Fri Jan 03, 2014 4:04 am

Now just simply convert them.
Attachments
compare-ary1.fsm
(465 Bytes) Downloaded 800 times
User avatar
TrojakEW
 
Posts: 111
Joined: Sat Dec 25, 2010 10:12 am
Location: Slovakia

Re: comparing 2 arrays (ruby)

Postby Nubeat7 » Fri Jan 03, 2014 9:09 am

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 }
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: comparing 2 arrays (ruby)

Postby tester » Fri Jan 03, 2014 11:36 am

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)

Postby tester » Fri Jan 03, 2014 1:28 pm

So this little schematic will detect which values in input array have changed (per cycle).
Attachments
detect-valchange.fsm
(743 Bytes) Downloaded 800 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


Return to General

Who is online

Users browsing this forum: No registered users and 81 guests