Sunday, October 30, 2011

PEBL Programming Tutorial I

This is Part I of a series of tutorials on PEBL
First  |   Previous  | Next

After an extended blogging break, during which I moved jobs and states, I've planned a series of posts in which I go through some of the basics of programming in PEBL.  This will cover some basic programming tutorials I've used during the Cognitive Science tutorial and another course I'm currently teaching.

So, to start, any PEBL program can be made by editing a text file.  On Windows, this can be done using notepad or wordpad, but a better option might be something like Notepad++, which is a "programmer's" editor and supports better marking of line numbers and such.   So, create a new text file, copy the text below, and save it with a name something like demo1.pbl.

define Start(p)
{
    #The # character is a comment character.
    #Anything on its line following it will be ignored.
    a <- 100
    b <- "One hundred" 
    c <- a +" = " + b
    #Creates a string '100 = One hundred'
  return c 

}



Above is a single function called Start.  Everything that happens within an experiment must be in a function, and when PEBL starts, it begins by running the function called Start().  You can create other functions that contain other code, but they will only get executed if they get run from Start or from a function that eventually gets executed from Start().

Also, notice that some of the lines start with a # character.  This is PEBL's comment character, and indicates that everything on that line should be ignored.  You can use it to add text to help describe what a piece of code is doing, or to remove or add specific parts to the program.

PEBL will move through each line of the Start function, one by one, and execute each line in turn.  The first two lines it gets to is:
    a <- 100
    b <- "One hundred" 
which tells PEBL to create what is called a variable in memory and store the value 100 in it.  The variable is named 'a', and can later be accessed through that name.    The next line assigns a text string "One hundred" to a variable named 'b'.

To use these values later, you can treat them just like the numbers and text used in these two lines.  The next line does just that:

    c <- a +" = " + b


On the right side, it combines the values in a and b, along with a text string " = ".  It will create a new string that reads "100 = One hundred", and assign it to the variable c.  Note that the + sign will combine text together when its arguments are text, and add numbers together.  When it gets a number and a text string, it combines them into a new string. 

This will run and create the memory locations where this string is located, but it will do nothing else.  It will not even create a window.  If you run the script, it might not even be apparent that it ran.  In the next tutorial post, we will take a look at how to do that.
Post a Comment