Ruby search and extract

For general discussion related FlowStone
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Ruby search and extract

Post 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!
Attachments
search words.fsm
(623 Bytes) Downloaded 918 times
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Ruby search and extract

Post 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
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby search and extract

Post by tulamide »

Here you are.
Attachments
search words[tula].fsm
(823 Bytes) Downloaded 981 times
"There lies the dog buried" (German saying translated literally)
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Ruby search and extract

Post by Nubeat7 »

.. but just use tulamide ones ;)
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Ruby search and extract

Post by adamszabo »

Wow, thank you guys, thats perfect!
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Ruby search and extract

Post 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
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby search and extract

Post 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
Attachments
search words[tula]2.fsm
(916 Bytes) Downloaded 923 times
"There lies the dog buried" (German saying translated literally)
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Ruby search and extract

Post 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.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby search and extract

Post 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.
"There lies the dog buried" (German saying translated literally)
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Ruby search and extract

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