Best Scripting TipsHello Community,
I started the scripting course that Monica Beals helped make: Introduction to the JMP Scripting Language - JMP User Community
Still learning, but I have a basic question
What is a scripting command/tip you feel everyone should know about or one you wish you knew earlier?
for example: The pickfile is a good one for people to know since it allows you to pick any file that you'd like to run a set of commands on.
Re: Best Scripting TipsThree things that I wish I had known earlier:
- Open() often lets a scripter skip the Pick File () scripting
- Alt+double click on any function in the script window takes you directly to the Scripting Index for that function
- Scripting the report layer is easier than it otherwise appears, especially with the recent addition of the Box Path section of the Properties viewer:
Re: Best Scripting TipsI'll add 3 to @Jed_Campbell's three:
- Under no circumstance should Current Data Table be used to set the current data table. Tables should be accessed explicitly through a variable reference instantiated when the table is created. Many scripters have a hard time keeping track of which table is the current table, particularly when there are lots of Joins, Stacks, or Subsets. On top of this, the table which is set as the current table has changed over the years depending on the operation. I have seen this having caused more scripts to break than probably any thing else.
- Don't mess with SendToReport. It's features are not well documented and there is (almost?) always a message that gets the job done more easily and makes your code more readable.
- Learn how to use expression handling (Substitute, Eval, Expr, and Name Expr). @joseph_morgan (here) and @drewfoglia (here and here) have Discovery material that does a great job talking about this concept. I have two recipes in the JSL Cookbook that touch on it here and here). It is a difficult subject to understand clearly. Once you've master it, however, you'll be able to do things that are not possible any other way. If it can't be done with expression handling, it can't be done in JSL.
Re: Best Scripting TipsNot to discount anything that's been said already, but @DonMcCormack point 3 is the single most critical thing you can learn next. Expression handling is incredibly powerful and gives you access to capabilities in JSL that are jaw-dropping. Once you realize everything in JSL is an expression, you start looking at your scripts completely differently. I'd study Joseph's and Drew's materials and work through their examples next.
After that, my personal favorites are:
- The new For Each() loops (and their relatives, Filter Each() and For Each Row()). Unless you need a script to run on really old JMP releases, I'd just forget the old For() loop exists.
- Related to 1, the short hand version of the index function: i.e., 1::10::2 would create a vector starting at 1 counting through 10 by 2's (would give you 1,3,5,7,9) that you could use in the For Each() loop.
- The Associative Array and how to work with it (including how it's treated by the For Each() loop).
- Learning to use matrices instead of data tables for everything possible ( and how to use them with the For Each() loop).
Re: Best Scripting TipsSince you’re new:
- Semicolons. If something breaks in JMP (or SAS), look here first. Easy fix and box to check off, and so frequently the culprit. Parentheses & brackets next. [1b. The log often cannot represent these run-on sentences properly. Usually look at the prior lines of code too.]
- Use Print() generously as you build/test your script. “Is this variable the value I expect at this stage of the loop”: Print(“Varname: ” || varname). “At what stage is the process hanging (e.g. infinite loop)?” Print(“Finished Step 3, Before Step 4”). [2b: if the project is something likely to keep growing, create a variable to use as an indicator flag (0/1 or T/F) and utilize If() statements to turn on your Print() statements when you want to debug.]
- Like DoE, start small. e.g. If you want to run 100 items through a For Each() loop, test the loop just using the first 2-4. Save the waiting and potential crashing.
Mike also has a blog series expanding on his thoughts here: https://community.jmp.com/t5/JMPer-Cable/The-1st-thing-I-wish-I-knew-when-I-started-scripting-There-are/ba-p/276580
Re the Eval/Expr functions, I haven’t written enough JSL to feel comfortable with these yet. But nothing in my coding experience has been as earthmoving as being able to use & (and &&& ….fond memories!) in SAS’s macro facility and so far I’ve thought of Eval/Expr as JSL’s parallel. (Context: most of my pre-JMP career was SAS programming but I’ve dabbled in things).
Re: Best Scripting TipsYour #1 item reminds me of a great trick everyone should know: Ctrl-[ selects everything in a parenthesis. This is very useful for debugging, checking for completeness, and especially for code modifications (copy/paste/replace).