New To JMP Scripting? “Hello World”: JSL Style

Convention suggests that programming languages should be introduced with a simple trivial  “Hello World” program.  So I thought I would respect this convention and introduce JSL using this principle.

First I need to open a script window in which I can write my JSL code:

File>New>Script (or the second icon on the toolbar):

new-script-window

My basic “Hello World” script consists of a single JSL statement:

hello-world

There are three methods for running the script:

  • Select the run script icon from the toolbar
  • Right-click and select run script from the context menu
  • Press <CTRL>-R

Whichever method I choose to execute the script, the result is a bit disappointing – nothing seems to happen.  In fact something does happen, but the results are being displayed in the JMP log window which is not visible unless I explicitly request it.  To view the log window I select Log from the JMP View menu:

logmenu

 

Now when I run the script I see that it produces the following log window output:

logoutput1

OK well that’s “Hello World” complete!  But I think I can take things just a little bit further to make it much more useful.

An essential characteristic of computer programs is the concept of generalisation.  Currently my script is only able to write a fixed message to the log window.  To generalise the script to display an arbitrary message I need to use variables:

          msg = “Hello World”;
          Write(msg);

The write statement will now write to the log window whatever value has been assigned to the msg variable.  The first line assigns the text string “Hello World” to the variable.

My script consists of two JSL statements.  That is clear because I have written them on separate lines, but this is just a convention – the following would work just as well:

          msg = “Hello World”;  Write(msg)

JMP knows that there are two JSL statements because of the semicolon.  When one JSL statement finishes and another one starts they need to be separated by a semicolon.  Technically this means that in my two-line example the second line didn’t need to be terminated by a semicolon – I could have written:

          msg = “Hello World”;
          Write(msg)

msg is an example of a variable.  A variable acts like a named address to reference a piece of memory that stores some information.  In this example it stores a text string so msg is a termed a “string variable.  Many computer languages have so-called “type casting” where the data type associated with the variable needs to be explicitly declared.  This is not the case in JMP.  Variables do not need to be declared in advance – they can be created on-the-fly and can contain the following types of information:

  • Numbers e.g. 1, 3.14, 1e3
  • Text e.g. “this is some text”
  • Object references e.g. data tables, a graph builder window, a regression model

Variables can also contain collections of data which may take the form of lists or matrices.

The second line of my script includes the word “Write”. Write is an example of a function.  Functions contain pre-packaged pieces of JMP functionality.  They perform specific tasks that I can  control through the use of arguments.  The Write function outputs text to the log window.  I provide an argument in the form of the msg variable to tell the function what to write.  The argument is always enclosed in brackets, and if there is more than one argument then they are separated with commas.

Here is an example of a function with no arguments:

    x = Pi();

This will assign the value of pi to the variable x.

Here is an example of a function with two arguments:

    x= Word(2, “Hello World”);

The text string “Hello World” is broken down into separate words and in this example the second word (“World”) is assigned to the variable x.

Broadly speaking functions fall into the following categories:

  • Character string manipulation
  • Mathematical manipulations
  • Date and time manipulations
  • Probability and statistical calculations
  • Graphical output
  • Utility / convenience functions

The result of using a functions is either

  • An action e.g. Write(x) will write the value of the variable x to the JMP log window
  • A value e.g. Sqrt(16) will returns the result 4
  • An object e.g. New Window creates a window object

I’m going to use the New Window function to make the “Hello World” script more appealing.  The syntax to create a window object is:

    New Window( title, contents )

Using this function I can create my new JMP-style “Hello World” script:

hw-code

And here is the output:

hw-output

Notice that the second argument (content) is itself the result of a function.  The contents of a window are composed of building blocks known as display boxes.  A “text box” displays text in a window, a “button box” displays a button, and so on.

If you are interested in learning more, here is an interesting walk-through of JSL development:

http://www.pega-analytics.co.uk/blog/oneway-advisor-step-1/

17 May 2020 Update – the above link gives you a walk-through for building a “oneway advisor”.  You will find this useful particularly if you plan to script JMP analysis (or graphical) platforms.  I am planning to make this walk-through into an online course.  If you would be interested in this please DM me on my twitter handle: @PegaAnalytics.

 

3 thoughts on “New To JMP Scripting? “Hello World”: JSL Style”

Leave a Reply

Your email address will not be published. Required fields are marked *