1

As my first real program in Lua, I'd like to create a molecular weight calculator (similar to the one here: http://www.lenntech.com/calculators/molecular/molecular-weight-calculator.htm )

I'd like some help with the first step of this program. My first step should be take the users input as a string, and than to split the string. I am looking at http://lua-users.org/wiki/StringLibraryTutorial and trying to figure out how to do this. When the user input is CH4 the program should split this string into C, and H4. Here is my code:

H   = 1.008
C   = 12.011
N   = 14.007
O   = 15.999

io.write("Enter molecular formula")
input = io.read()

result = 
print("the molecular weigth is" .. result)

Can someone show me how I can accept the user input as a string and how to split that string?

Edit: as requested, I have been more specific in my exact question

Far Zin
  • 121
  • 4
  • Your questions show that you have not put sufficient effort into learning the basics of Lua. Follow the first few pages of "Programming in Lua" book (first edition is free online at Lua.org). Also when you post a question you should only include what is essential: listing the weight of 105 chemical elements is clearly not necessary, just list a few and viewers will get the idea.. – Oliver Apr 03 '14 at 04:43
  • 1
    Suppose I give the input `NaLi`. My program may also think it as `NAlI` (I know neither of those exist (as far as I can tell)) – hjpotter92 Apr 03 '14 at 05:38
  • @Schollii I have since edited the question, and hopefully it is a short enough, and specific enough question now. – Far Zin Apr 03 '14 at 11:43
  • I think you should accept the answer you got, read the PatternsTutorial (link is on the String tutorial page you mention) carefully, try the examples there, try a few things for your own problem and if you are having trouble with the splitting, create a new question where you show what you have tried. One you've figured out the splitting you can apply what You has suggested. – Oliver Apr 03 '14 at 12:20

1 Answers1

4

Here's a possible way that you can start with:

  1. Get the input string from standard input:

    input = io.read()
    
  2. Convert the string like Cr(NO2)2 to a string Cr+(N+O*2)*2. The nature of chemical element names is that it all begins with a uppercase letter, and followed by zero or more lowercase letters, so the rule could be: whenever a uppercase letter or a "(" is encountered (except when it's the first or preceded by a "(" ), insert a "+" before it, whenever a number is encountered, insert a "*" before it.

  3. Calculate the result from the string Cr+(NO*2)*2, that's the beauty of Lua, it's a legal Lua expression, so just load the string and get the result:

    str = "Cr+(N+O*2)*2"
    func = assert(load("result = " .. str))
    func()
    
    print("the molecular weigth is" .. result)
    

In Lua 5.1, use loadstring() in the place of load().

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • This is a good summery of what I need to do. It'd work if I had more knowledge in Lua. However, can you show me the first few steps within step 1? For example, how to identify `input` as string. and perhaps how to break up string. – Far Zin Apr 03 '14 at 03:54
  • 1
    @FarZin Unless using with parameter `"*n"` (which isn't used here), the value you get from `io.read()` is a string. It's not a small program, do it step by step from the simplest cases. The string processing is the most complicated part, I don't know if I missed some cases in the rules though. – Yu Hao Apr 03 '14 at 04:24