Page 1 of 2
Ruby search and extract
Posted: Thu Jan 26, 2017 11:08 am
by adamszabo
Hey guys, I need a hand with something, I tried to solve it by myself but its a bit beyond me.
Here is the scenario: I have a text file with words and a number like so:
cat=4
dog=5
fish=7
Then I have multiple string inputs and the same number of integer outputs. If I write the correct word in one of the inputs it should spit out the correct number at the opposite output. So if I write cat in input 1, it should be 4 at the output. If I write fish, it should be 7, but if it cant find the matching word, so I write 'horse' it will write -1.
I only managed with 1 input, and my first problem is that it doesnt even look for the correct word. So I have:
Osc 2 Control 1=4
But even if I just write 'Osc 2' is still think its correct and outputs 4. It should only output 4 when the string is 'Osc 2 Control 1'.
I think I would need a big loop which checks all my inputs and outputs all results at the same time.
Any help would be much appreciated!
Re: Ruby search and extract
Posted: Thu Jan 26, 2017 1:39 pm
by Nubeat7
if you need just the last one you can do this
Code: Select all
@text.each_line do |l|
case
when l.match(@name1)
output 0, l.chomp[-1]
when l.match(@name2)
output 1, l.chomp[-1]
when l.match(@name3)
output 2, l.chomp[-1]
end
end
Re: Ruby search and extract
Posted: Thu Jan 26, 2017 1:44 pm
by tulamide
Here you are.
Re: Ruby search and extract
Posted: Thu Jan 26, 2017 1:47 pm
by Nubeat7
.. but just use tulamide ones

Re: Ruby search and extract
Posted: Thu Jan 26, 2017 1:50 pm
by adamszabo
Wow, thank you guys, thats perfect!
Re: Ruby search and extract
Posted: Thu Jan 26, 2017 2:10 pm
by adamszabo
Just one minor detail, when I change something inside the big text file, it doesnt seem to update the outputs? I wrote and even trigger on the text but doesnt do anything:
Code: Select all
def event i, v
if i == 'text'
h = Hash[@text.each_line.map { |l| l.chomp.split('=', 2) }]
if h.has_key?(v)
output i.index, h[v]
else
output i.index, -1
end
end
end
Re: Ruby search and extract
Posted: Thu Jan 26, 2017 2:29 pm
by tulamide
Yes, that's correct. Since the event wasn't one of the 'active' inputs there's no output. But if you trigger one of the 'active' inputs afterwards, it will reflect the changes. So, while the text change is noticed from the module, you just don't see it. It is much more effort to make it work both ways.
Are you sure you need it to be interactive? If so, use this version and make sure to add a new trigger connection for each new input you create!
EDIT: Damn, I was too fast. Please change this code
Code: Select all
if i == "text"
output "trig", nil
end
to this one:
Code: Select all
if i == "text"
output "trig", nil
return
end
Re: Ruby search and extract
Posted: Fri Jan 27, 2017 12:07 pm
by adamszabo
Yes, perhaps I should have mentioned, that I will be reading/writing to the text file, and I need to get the data from there. Thats why the other inputs are static, and the text file will be changing. I will try your solution, although there will be more than 50 inputs, I hope it wont break haha.
Re: Ruby search and extract
Posted: Fri Jan 27, 2017 1:28 pm
by tulamide
adamszabo wrote:Yes, perhaps I should have mentioned, that I will be reading/writing to the text file, and I need to get the data from there. Thats why the other inputs are static, and the text file will be changing. I will try your solution, although there will be more than 50 inputs, I hope it wont break haha.
You mean you need it the other way 'round? Or in other words, which inputs will change throughout use, and which ones won't?
If it's just reversed order (textfile will change only), that's doable in an easier way. The trigger was just a quick workaround for a situation where ALL inputs change.
Re: Ruby search and extract
Posted: Fri Jan 27, 2017 1:34 pm
by adamszabo
The 'name' inputs will not change so for example 'Osc 2 Control 1' will never change, its always going to be like that. Its the 'text' input that will constantly change. So the number after the equal sign in the text 'Osc 2 Control 1=0' can change to 0,1,2,3 or whatever number. If the text is empty then all integer outputs will be -1 since no info was found for any control.