It’s been a while – so, since it’s Friday, here is a collection of Friday’s Functions … some of my favourite user-defined functions.
The JMP website has introduced a similar theme, JSL Cookbook, so probably I will change the tag associated with these posts to be JSL Cookbook instead of Friday’s Functions.
Abbreviated File Path
Converts a full path into an abbreviated form for display purposes. The abbreviated form looks like this: “. . . / Demo / Scripts”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Abbreviated File Path = Function({path},{Default Local}, delim = "/"; If (Contains(path,"\"), delim = "\" ); lstWords = Words(path,delim); nwords = NItems(lstWords); If (nWords>2, abbrPath = "..." || delim || lstWords[nwords-1] || delim || lstWords[nwords] , abbrPath = path ); Return(abbrPath); ); |
Column Exists
Before referring to a column its helpful to know that it exists. This function takes two arguments: a reference to the data table and the string literal name of the column.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Column Exists = Function({dt,colName},{Default Local}, Try( lstNames = dt << Get Column Names(string); For (i=1,i<=NItems(lstNames),i++, lstNames[i] = Uppercase(lstNames[i]) ); pos = Loc(lstNames,Uppercase(colName)); If (NRows(pos)==0, retVal = 0 , retVal = 1 ) , retVal = 0 ); // return retVal ); |
Copy Table Variables
Sometimes its useful to preserve table variables when creating a new table. The function takes arguments that reference the source and destination data tables.
1 2 3 4 5 6 7 8 9 10 11 |
Copy Table Variables = Function({dtSource,dtDest},{Default Local}, dtDest = Current Data Table(); lstNames = dtSource << Get Table Variable Names; For (i=1,i<=NItems(lstNames),i++, varName = lstNames[i]; varValue = dtSource << Get Table Variable(varName); dtDest << Set Table Variable(varName,varValue) ); ); |
JMP Version Number
Sometimes code needs to be tweaked based on the version of JMP. This function returns the JMP version as a number e.g. 13.2.
1 2 3 4 5 6 7 8 9 10 11 12 |
JMP Version Number = Function({},{Default Local}, str = JMP Version(); w = Words(str,"."); major = w[1]; minor = w[2]; strVersion = major || "." || minor; version = Num(strVersion); // return version ); |
Get File Directory
Given a path to a file this function returns a path to the directory that contains the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Get File Directory = Function({path},{Default Local}, dir = Convert File Path(path,"absolute","windows"); lst = {}; testExpr = Expr( //show(str); InsertInto(lst,str); 0 ); pattern = Pat Fence() + PatArb()>>str + "\" + PatTest( testExpr ); Pat Match(dir,pattern); n = NITems(lst); If (n>1, retVal = lst[n] , retVal = "" ); Return(retVal); ); |
Namespace Contains Variable
Once you start using namespaces its hard to know how you lived without them. This utility allows me to check whether a namespace contains a specific variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Namespace Contains Variable = Function({ns,varName},{Default Local}, retVal = 0; Try( Eval(Parse(Eval Insert("\[ value = ns:^varName^ ]\"))); retVal = 1 ); // return retVal ); |
Day Of Week As String
So you’ve got one of these weird numbers that is a datetime. But what day of the week does it represent?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Day Of Week As String = Function({datetime},{Default Local}, Choose( Day Of Week(datetime), "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", ) ); |
Long Date String
Creates a formatted date string e.g. Monday, 20 April 2018. The argument monthFormat should be “long month” or “short month”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Long Date String = Function({datetime,monthFormat},{Default Local}, dayWord = Day Of Week As String(datetime); dayNum = Day(datetime); If (dayNum < 10, dayNum = "0" || Char(dayNum) ); Match( LowerCase(monthFormat), "short month", thisMonth = Month Of Year As Short String(datetime), "long month", thisMonth = Month Of Year As String(datetime), Assert("Long Date String", "Invalid month format: should be 'long month' or 'short month'" ) ); thisYear = Year(datetime); str = Eval Insert("\[^dayWord^, ^dayNum^ ^thisMonth^ ^thisYear^]\"); str ); |
Month Of Year As String
Another datetime utility. The purpose of a function should be obvious from its name; hopefully that is true in this case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Month Of Year As String = Function({datetime},{Default Local}, Choose( Day Of Week(datetime), "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ) ); |
Window Exists
Sometimes a user can upset your plans by closing a window that you are relying on to be open. This function can help:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Window Exists = Function({title},{Default Local}, w = Window(title); // if the window exists w is a displaybox, // otherwise it is an empty list If (IsList(w), retVal = 0 , retVal = 1 ); retVal ); |
List Contains Item
Sometimes you think there should be an easier way of doing things …
1 2 3 4 5 6 7 8 9 10 11 12 |
List Contains Item = Function({lst,value},{Default Local}, rows = Loc(lst,value); If (NRows(rows)==0, retVal = 0 , retVal = 1 ); // return retVal ); |
NICE! GREAT! HELPS A LOT!