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
another ruby and arrays thing
7 posts
• Page 1 of 1
another ruby and arrays thing
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?
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.
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
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:
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
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
- TheOm
- Posts: 103
- Joined: Tue Jan 28, 2014 7:35 pm
- Location: Germany
Re: another ruby and arrays thing
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.
Thanks.
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: another ruby and arrays thing
Small problem here.
How to adjust it to have multiple arrays pass through that way?
(and how to fix it?)
How to adjust it to have multiple arrays pass through that way?
(and how to fix it?)
- Attachments
-
- issue.fsm
- (583 Bytes) Downloaded 723 times
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: another ruby and arrays thing
I would do it like this
- Attachments
-
- k.fsm
- (538 Bytes) Downloaded 930 times
- TheOm
- Posts: 103
- Joined: Tue Jan 28, 2014 7:35 pm
- Location: Germany
Re: another ruby and arrays thing
Thanks!
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
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 46 guests