filtering array of strings (ruby)

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

filtering array of strings (ruby)

Post 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?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
TheOm
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: filtering array of strings (ruby)

Post 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.
User avatar
CoreStylerz
Posts: 327
Joined: Sun Jan 22, 2012 2:19 am
Location: italy
Contact:

Re: filtering array of strings (ruby)

Post 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).
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.
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: filtering array of strings (ruby)

Post 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.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
TheOm
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: filtering array of strings (ruby)

Post 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
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: filtering array of strings (ruby)

Post by tester »

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.
Post Reply