LuaStart
LuaStart

Reputation: 407

Lua if A==1 or 2 or 3 then

I have a lot of music chords that can have alternate names, rather than having to create longer lines with a lot of == and or's for each alternate name, and if chord=="Maj" or "maj" or.. don't work with Lua:

if chord=="Maj9" or chord="maj9" or chord=="M9" or chord=="Maj7(add9)" or chord=="M7(add9)" then notes="0,4,7,11,14" end

I need a simpler way to do it, maybe just reformat the lines in Notepad++ to use an array, at the moment each of the 200+ chords on one line each:

if chord=="Maj9,maj9,M9,Maj7(add9),M7(add9)" then notes="0,4,7,11,14" end
if chord=="mMaj7,minmaj7,mmaj7,min/maj7,mM7,m(addM7),m(+7),-(M7)" then notes="0,3,7,11" end

Upvotes: 1

Views: 405

Answers (1)

brianolive
brianolive

Reputation: 1671

The correct way is to normalize your input. For example, take whatever chord value comes in and use Lua’s string.lower() function to make the string all lowercase. By normalizing your input, you simplify the logic you need to write to work with that data. Consider other ways as well to normalize the data. You might, for example, write a method that converts all notes into an enumerated list (C = 1, C# = 2, etc.). That way equivalent notes get the same in-memory values.

Those are just a few ideas to get you on track. You should not try to think up and then hard-code every possible way a user may input a chord name.

Upvotes: 3

Related Questions