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

filtering array of strings (ruby)

For general discussion related FlowStone

filtering array of strings (ruby)

Postby tester » Mon Jun 30, 2014 10:22 pm

I'm stuck with something, and conceptually I found different way do deal with it.

I'm looking for following solution.

Let say that I have array of strings, like this

aaxxxaayyy
abqqqcdwww
...

Now - I'd like to compare two sections within each string in the array, and if these sections match - output 0, if don't match - output 1. Thus array of 0's and 1's will be on output.

For above example - first string contains matching pair (lenght = 2, start1 = 0, start2 = 5):

aaxxxaayyy

How to make it?
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: filtering array of strings (ruby)

Postby TheOm » Tue Jul 01, 2014 3:09 am

If I understood you right, it could be done like this:

Code: Select all
result_array = string_array.map do |str|
   str =~ /^(..)...\1/ ? 0 : 1
end


I suggest reading this. It's about regular expressions in ruby.
TheOm
 
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: filtering array of strings (ruby)

Postby CoreStylerz » Tue Jul 01, 2014 10:20 am

Best way is to join arrays removing duplicates(union) get common elements(intersect) and then check differences by removing all elements in array1 contained in array2(difference).
Need my support for app development, website or custom scripts?
PM me if you are interested.
Experienced Java, J2EE, PHP, Javascript, Angular, Cloud Solutions developer.
User avatar
CoreStylerz
 
Posts: 327
Joined: Sun Jan 22, 2012 2:19 am
Location: italy

Re: filtering array of strings (ruby)

Postby tester » Tue Jul 01, 2014 1:05 pm

I may have messed something last night, so I re-type. :-) (It's a part of a project, that I left here somewhere around, and I'm rebuilding it into ruby due to multiple array operations).

Input array will be something like this:

f1a->f1a/a2
f1a->f5b/a3
f3c->f3c/d2
f3b->f5a/a3

Output array should be:

0
1
0
1

String to find and compare within lines is 3 elements long, first element starts at fixed position N1, second element starts at fixed position N2.

Output array feeds an external module with multiple inputs to sync.
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: filtering array of strings (ruby)

Postby TheOm » Tue Jul 01, 2014 2:00 pm

Here, this will work and you can also change your n1, n2 and len if you need to.

Code: Select all
n1=0 #position of first occurence
n2=5 #position of second occurence
len=3 #length of the string to find and compare
output_array = string_array.map do |str|
    str =~ /^.{#{n1}}(.{#{len}}).{#{n2-n1-len}}\1/ ? 0 : 1
end
TheOm
 
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: filtering array of strings (ruby)

Postby tester » Sat Jul 05, 2014 12:34 am

Thanks, sorry for delay in my response, busy days, will look into it tomorrow.
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 62 guests