Page 1 of 1
filtering array of strings (ruby)
Posted: Mon Jun 30, 2014 10:22 pm
by tester
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?
Re: filtering array of strings (ruby)
Posted: Tue Jul 01, 2014 3:09 am
by TheOm
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.
Re: filtering array of strings (ruby)
Posted: Tue Jul 01, 2014 10:20 am
by CoreStylerz
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).
Re: filtering array of strings (ruby)
Posted: Tue Jul 01, 2014 1:05 pm
by tester
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.
Re: filtering array of strings (ruby)
Posted: Tue Jul 01, 2014 2:00 pm
by TheOm
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
Re: filtering array of strings (ruby)
Posted: Sat Jul 05, 2014 12:34 am
by tester
Thanks, sorry for delay in my response, busy days, will look into it tomorrow.