Page 1 of 1
another ruby and arrays thing
Posted: Sun Jul 06, 2014 1:19 pm
by tester
I just realized, that I don't have one solution under my hand, and I'm using wrong one.
Let say that I have 2 arrays:
array1 (strngs):
string1
string2
string3
string4
array2 (boolean ints):
0
1
0
1
On output I'd like to have only rows that correspond to 1's.
For above example, output array it would be:
string2
string4
How to make it in ruby?
Re: another ruby and arrays thing
Posted: Sun Jul 06, 2014 2:46 pm
by TheOm
I assume this is related to your other thread?
Do you only want the strings that don't match in your new array?
input:
[ "f1a->f1a/a2",
"f1a->f5b/a3",
"f3c->f3c/d2",
"f3b->f5a/a3" ]
result:
["f1a->f5b/a3",
"f3b->f5a/a3" ]
Then use select instead of map, like this:
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.select do |str|
!(str =~ /^.{#{n1}}(.{#{len}}).{#{n2-n1-len}}\1/)
end
Re: another ruby and arrays thing
Posted: Sun Jul 06, 2014 2:55 pm
by TheOm
If that's not what you want, you could do something like this:
Code: Select all
array1.each_index do |i|
array1.delete_at(i) if array2[i] == 0
end
Re: another ruby and arrays thing
Posted: Sun Jul 06, 2014 3:33 pm
by tester
Second solution will be more appropriate; it applies to different sections. There are various filters (formula & range based, content or comparison based) which define whether to pass values/rows/strings (1) or not (0), and such module outputs appropriate entries according to 1's indexes.
Thanks.
Re: another ruby and arrays thing
Posted: Sun Jul 06, 2014 6:21 pm
by tester
Small problem here.
How to adjust it to have multiple arrays pass through that way?
(and how to fix it?)
Re: another ruby and arrays thing
Posted: Sun Jul 06, 2014 6:55 pm
by TheOm
I would do it like this
Re: another ruby and arrays thing
Posted: Mon Jul 07, 2014 10:23 am
by tester
Thanks!