LUA and string matching

Home Forums General Programming LUA and string matching

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #501
    dejf_mk
    Participant
      • Topics: 5
      • Replies: 10
      • Total: 15

      Ok so today’s request for help is related to dealing with strings in LUA. What I’m trying to do is have a method react differently depending on the modulator which calls the method. I’ve worked out that I can get the modulator name, checked the API docs for String and read through the linked page on the JUCE API site. But I still just can’t get it to work!

      Here’s my first stab at some code:
      [code:2f2lmgt1]
      function myTestButton2 (modulator)
      modLuaName=modulator:getLuaName()

      if (modLuaName:endsWith("sustain-on")) then
      console("Received the requested modulator")
      end

      end[/code:2f2lmgt1]

      And the button’s modulator name is OP1-sustain-on, but this throws an "attempt to call method ‘endsWithChar’ (a nil value)" exception.

      I’m sure this is just a silly noob mistake but I’m stuck, any ideas?

      David

      #3522
      atom
      Keymaster
        • Topics: 159
        • Replies: 2945
        • Total: 3104
        • ★★★★★

        endsWithChar is a different method than endsWith so witch one you are using ?

        #3523
        dejf_mk
        Participant
          • Topics: 5
          • Replies: 10
          • Total: 15

          Hi atom,
          Aha yes sorry I must have made a mistake, I want to use the endsWith method and match a whole substring to the modulator’s name string.

          #3524
          dejf_mk
          Participant
            • Topics: 5
            • Replies: 10
            • Total: 15

            I don’t want to be impatient, but is there anyone who has any idea how to handle this in Ctrlr’s LUA? <img decoding=” title=”Smile” />

            #3525
            atom
            Keymaster
              • Topics: 159
              • Replies: 2945
              • Total: 3104
              • ★★★★★

              It’s a bit complicated but doable, i think i messed this up by not registering a converter for the String class, you can do this now like so
              [code:25izf3m3]
              String name = modulator:getLuaName()
              if (name:endsWith(String("sustain-on"))) then
              console ("match")
              end
              [/code:25izf3m3]

              this is because LUA expects it’s string parameters to by of type String and luabind uses std::string and it can’t convert them, i’ll try to work around this somehow.

              #3526
              atom
              Keymaster
                • Topics: 159
                • Replies: 2945
                • Total: 3104
                • ★★★★★

                Also you don’t really NEED to rely on the JUCE api there is a whole lot of LUA functions that are faster and simpler for stuff like MATH and STRING for example:
                [code:3f7xz4gp]
                m = panel:getModulatorByName("modulator-3")
                name = m:getLuaName()
                ret = string.find (name, "-3")
                console ("ret="..ret)
                — will print 10 as the substring -3 is at the 10th position
                [/code:3f7xz4gp]

                Here is the whole string library docs: http://lua-users.org/wiki/StringLibraryTutorial

                LUA is a powerful programming engine, use it.

                #42048
                audiosync
                Participant
                  • Topics: 5
                  • Replies: 13
                  • Total: 18

                  It’s a bit complicated but doable, i think i messed this up by not registering a converter for the String class, you can do this now like so
                  [code:25izf3m3]
                  String name = modulator:getLuaName()
                  if (name:endsWith(String(“sustain-on”))) then
                  console (“match”)
                  end
                  [/code:25izf3m3]

                  Hi, I just stumbled over the same issue.
                  When I try the above console shows error for the string declaration:

                  ERROR: [string “change_form”]:8: ‘=’ expected near ‘name’

                  
                  change_form = function(mod, value)
                  
                  	String name = mod:getLuaName()
                  	if (name == "v1_op1_form") then
                  		console ("match")
                  	end
                  
                  • This reply was modified 9 years, 1 month ago by audiosync.
                  #42322
                  atom
                  Keymaster
                    • Topics: 159
                    • Replies: 2945
                    • Total: 3104
                    • ★★★★★

                    This is not c++ or c.

                    you do

                    name = mod:getLuaName()

                    you never do
                    String name = mod:getLuaName()

                    this is a syntax error for lua

                    if you want to declare an empty String you do

                    myString = String()

                    variables in Lua have no types, you never specify them.

                    #42355
                    audiosync
                    Participant
                      • Topics: 5
                      • Replies: 13
                      • Total: 18

                      Ok, thanks for the info!
                      So how do I get the calling modulator’s name into a string?
                      When I tried this:

                      
                      change_form = function(mod, value)
                      
                      	name = mod:getLuaName()
                      	v = string.sub (name, 2, 2)
                      	console ("Voice: "..v)
                      
                      end
                      

                      …I always ran into the error:
                      Error message: [string “change_form”]:12: bad argument #1 to ‘sub’ (string expected, got userdata)

                      #43061
                      audiosync
                      Participant
                        • Topics: 5
                        • Replies: 13
                        • Total: 18

                        How silly! I tried to read getLuaName() into string because this didn’t work:
                        vop = (panel:getModulatorByName(“modulator1”):getComponent():getProperty("name"))

                        but I just had to do this:
                        name = (panel:getModulatorByName(modulator1):getProperty("name"))

                        sorry for disturbing…

                        • This reply was modified 9 years ago by audiosync.
                      Viewing 10 posts - 1 through 10 (of 10 total)
                      • The forum ‘Programming’ is closed to new topics and replies.
                      There is currently 0 users and 72 guests online
                      No users are currently active
                      Forum Statistics
                      Threads: 2,495, Posts: 17,374, Members: 77,605
                      Most users ever online was 12 on January 22, 2019 3:47 pm
                      Ctrlr