another ruby and arrays thing

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

another ruby and arrays thing

Post 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?
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: another ruby and arrays thing

Post 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
TheOm
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: another ruby and arrays thing

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

Re: another ruby and arrays thing

Post 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.
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: another ruby and arrays thing

Post by tester »

Small problem here.

How to adjust it to have multiple arrays pass through that way?

(and how to fix it?)
Attachments
issue.fsm
(583 Bytes) Downloaded 887 times
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: another ruby and arrays thing

Post by TheOm »

I would do it like this
Attachments
k.fsm
(538 Bytes) Downloaded 1087 times
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: another ruby and arrays thing

Post by tester »

Thanks!
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Post Reply