Support

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

Ruby search and extract

For general discussion related FlowStone

Ruby search and extract

Postby adamszabo » Thu Jan 26, 2017 11:08 am

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 862 times
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Ruby search and extract

Postby Nubeat7 » Thu Jan 26, 2017 1:39 pm

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
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: Ruby search and extract

Postby tulamide » Thu Jan 26, 2017 1:44 pm

Here you are.
Attachments
search words[tula].fsm
(823 Bytes) Downloaded 910 times
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby search and extract

Postby Nubeat7 » Thu Jan 26, 2017 1:47 pm

.. but just use tulamide ones ;)
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: Ruby search and extract

Postby adamszabo » Thu Jan 26, 2017 1:50 pm

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

Re: Ruby search and extract

Postby adamszabo » Thu Jan 26, 2017 2:10 pm

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
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Ruby search and extract

Postby tulamide » Thu Jan 26, 2017 2:29 pm

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 861 times
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby search and extract

Postby adamszabo » Fri Jan 27, 2017 12:07 pm

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.
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Ruby search and extract

Postby tulamide » Fri Jan 27, 2017 1:28 pm

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

Re: Ruby search and extract

Postby adamszabo » Fri Jan 27, 2017 1:34 pm

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.
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Next

Return to General

Who is online

Users browsing this forum: No registered users and 68 guests