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)
6 posts
• Page 1 of 1
filtering array of strings (ruby)
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?
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.
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)
If I understood you right, it could be done like this:
I suggest reading this. It's about regular expressions in ruby.
- 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)
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.
PM me if you are interested.
Experienced Java, J2EE, PHP, Javascript, Angular, Cloud Solutions developer.
-
CoreStylerz - Posts: 327
- Joined: Sun Jan 22, 2012 2:19 am
- Location: italy
Re: filtering array of strings (ruby)
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.
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.
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)
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)
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.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
6 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 102 guests