Since this is a journal you may find starting from earlier articles helpful. I have covered a bit about the science, the FoldIT user interface, GUI recipes, and Script recipes. If you give me hints where I could be helpful I will focus in that general direction to my discretion. Currently I am going though the basic concepts of LUA script recipes. Once I get past intoductory LUA scripting I can start exploring the science of folding proteins by using LUA scripts.

Thursday, October 7, 2010

Comments, Variables, and Constants.

Programs can be quite mysterious.  Programmers try to help each other by putting comments into their code.  LUA has two varieties of comments.  The first variety starts with two dashes (--) and ends when the line ends.  The second variety starts with two dashes and two open brackets (--[[) and ends with two close brackets and two dashes (]]--).  Here's a small script you can run to see the behavior of commnets:

  print('before dashes')  -- print('after dashes') anything I put here is a comment
  print('new line after comment')
  --[[ print('line 1 of comment')
  print('line 2 of comment') ]]--
  print('after the second comment')
  --[[ the normal use of multiline comments is to temporarily disable code
         or make multiline comments when one doesn't want dashes at the beginning of the line.
  print('this is the normal use of multiline comments)'
  ]]--
  print('after third comment')

Trying code helps me verify my understanding.  If you aren't sure you understand comments please create a new script, copy the text from above into it and run the script.  Then modify it to prove your understanding.  If you put something the computer doesn't understand outside a comment the recipe output will show an error.  I added this line to the end of the script above:

this throws an error

The recipe output window displayed:

ERROR:[string "print('before dasses') -- print('after dashes') anything I put..."]:20: '=' expected near 'thows'

The error message displays the first line of the script followed by a colon followed by the line number on which the error happened followed by a colon followed by the computers best guess as to the problem.  The computer is seldom right about the cause of the error but is always right that there is an error in the program.  In this case the computer is assuming the programmer wanted to assign an object to the variable "this".

Last time I said Variables name objects.  LUA has several ways to assign a name to an object, the most basic of which is an assignment statement.  An assignment statement has the variable on the left of an equal sign (=) and the definition of the object on the right of the equal sign.  A variable can be redefined as often as one wishes.  Here are a few somewhat nonsense examples you can test:

  --exploring the assignment statement
  x='a string constant'
  x = 'the equal sign can have zero or more spaces on either side of it'
  x=3 -- numbers can be integers or reals.  3 is an integer constant
  x=.01 -- .01 is a real constant.
  y= x * 100 -- * is the LUA infix multiply function.  The definition is evaluated and the result assigned.
  print('x is ', x, ' and y is ', y)
  z=print -- you can assign your own name to functions
  z('yes this will print')
  aStructure = {} -- structures are complex enough to warrant its own article. 

OK, I didn't get to data types this morning.  I'm not sure of my audience so I'm going into greater detail than I otherwise would.  As I said last time, one has to power through some basic concepts even if they are a bit confusing at first.  The best way to learn is to try things out and see what they do.  Try the code on your own and modify it based upon your understanding.  If you create a new script it will be unnamed.  I have a recipe named test I modify and run.  Remember to set info and save before running a modified recipe.

I've mentioned assignment to strings, integers, reals, and functions.  Next time I'll go a bit deeper into structures and reintroduce the concept of scope.

Have fun folding.

No comments:

Post a Comment