Associative Array
Associative Array using New Namespace
Summary: Processes retrieving and displaying earthquake data from the USGS website, utilizing HTTP requests and JSON parsing to generate a data table.
Code:
//
//https://earthquake.usgs.gov/fdsnws/event/1/
Names Default To Here( 1 );
New Namespace(
"earthquake"
);
earthquake:request = New HTTP Request();
request_headers = Associative Array();
earthquake:request <<
Reset(
Headers( request_headers ),
Url(
"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2019-02-01&endtime=2019-03-04&minmagnitude=2"
),
Method( "Get" )
);
data = earthquake:request << Send();
dt = JSON To Data Table( data );
dt << show window();
Code Explanation:
- Set default names.
- Create new namespace.
- Initialize HTTP request.
- Create associative array for headers.
- Reset HTTP request with headers and URL.
- Set method to GET.
- Send HTTP request.
- Convert JSON response to data table.
- Display data table window.
Associative Array using Function
Summary: Retrieve and filter enabled platforms for scriptable commands, excluding specific platforms and generating a list of platform samples.
Code:
excluded_platforms = {"Tabulate", "DOE", "Model Dialog", "Formula Depot", "Diagram", "Fit Group", "Functional Data Explorer Group",
"Model Screening", "Data Feed", "Scheduler"};
get_preset_enabled_platforms = Function( {excluded_platforms},
{Default Local},
dt = Show Commands( "Scriptables" );
r = (dt << Get Rows Where( :Command == "New Preset" ));
platforms = dt:Component[r];
Close( dt, NoSave );
platforms = Associative Array( platforms );
platforms << Remove( Associative Array( excluded_platforms ) );
platforms << Get Keys;
);
get_preset_samples = Function( {platforms},
{Default Local},
plat_sample_file_list = Words( Load Text File( "$SAMPLE_DATA/../ Presets/Presets-en.jmppresets" ), "\!N," );
plat_samples = [=> {}];
current_plat = "";
For Each( {line, l}, plat_sample_file_list,
If( Starts With( Trim Whitespace( line ), "Label(" ),
Try( Log Capture( label_arg = Arg( Parse( line ), 1 ) ), Continue() );
If(
Contains( platforms, label_arg ),
current_plat = label_arg;
plat_samples[current_plat] = {};
Continue();,
Starts With( Trim Whitespace( plat_sample_file_list[l + 1] ), "JSL" ),
plat_samples[current_plat] ||= Eval List( {label_arg} )
);
)
);
plat_samples;
);
preset_enabled_platforms = get_preset_enabled_platforms( excluded_platforms );
plat_samples = get_preset_samples( preset_enabled_platforms );
Code Explanation:
- Define excluded platforms.
- Define function
get_preset_enabled_platforms. - Retrieve scriptable commands.
- Filter rows for new presets.
- Extract component names.
- Close scriptable commands window.
- Convert components to associative array.
- Remove excluded platforms.
- Retrieve keys from filtered platforms.
- Define function
get_preset_samples. - Load platform preset file.
- Initialize platform samples dictionary.
- Iterate through preset file lines.
- Check for label line.
- Capture label argument.
- Check if platform is enabled.
- Set current platform.
- Add platform to samples.
- Check for JSL line.
- Add sample to current platform.
- Return platform samples.
Example 1
Summary: Calculates and stores mean and standard deviation values for specified columns in a data table, allowing for easy retrieval and analysis.
Code:
dt = Open("data_table.jmp");
colNames = {"height", "weight"};
aa = Associative Array();
colAA = dt << New Column( "AA Expression", Expression );
For( i = 1, i <= N Items( colNames ), i++,
aa[colNames[i]] = Eval List( {Col Mean( Column( colNames[i] ) ), Col Std Dev( Column( colNames[i] ) )} )
);
colAA << Set Each Value( aa );
colAA << Get contents;
Code Explanation:
- Open data table.
- Define column names.
- Initialize associative array.
- Create new column for expressions.
- Loop through each column name.
- Calculate mean and standard deviation.
- Store results in associative array.
- Assign associative array to column.
- Retrieve column contents.
- End script.
Example 2
Summary: Opens a data table and creation of an associative array to store window title information.
Code:
dt = Open("data_table.jmp");
befAA = Associative Array( Window() << get window title );
Code Explanation:
- Open data table.
- Create associative array.
- Store window title.
Example 3
Summary: Process of summarizing data by age, height, sex, and weight, generating a list of unique keys and filtering data tables based on 'PValues' for Windows hosts.
Code:
dt = Open("data_table.jmp");
bef aa = Associative Array( Window() << get window title );
obj = Summarize YByX( X( :age, :height ), Y( :sex, :weight ) );
aft aa = Associative Array( Window() << get window title );
aft aa << Remove( bef aa );
aftlst = aftaa << get keys;
For( i = 1, i <= N Items( aftlst ), i++,
If( Host is( Windows ),
If( Contains( aftlst[i], "PValues" ),
dt1 = Data Table( aftlst[i] )
),
If( Contains( aftlst[i], "PValues" ),
dt1 = Data Table( aftlst[i] )
)
)
);
mat1 = dt1 << get as matrix;
Close( dt1, no save );
Close( dt, no save );
ut relative epsilon = 1e-10;
Code Explanation:
- Open data table.
- Create associative array before summary.
- Perform summary analysis.
- Create associative array after summary.
- Remove common elements from associative arrays.
- Get unique keys from associative array.
- Loop through unique keys.
- Check if host is Windows.
- Check if key contains "PValues".
- Assign matching data table to dt1.
Example 4
Summary: Process of summarizing data by age, height, sex, and weight, generating a matrix with PValues for Windows hosts.
Code:
dt = Open("data_table.jmp");
bef aa = Associative Array( Window() << get window title );
obj = Summarize YByX( X( :age, :height ), Y( :sex, :weight ) );
aft aa = Associative Array( Window() << get window title );
aft aa << Remove( bef aa );
aftlst = aftaa << get keys;
For( i = 1, i <= N Items( aftlst ), i++,
If( Host is( Windows ),
If( Contains( aftlst[i], "PValues" ),
dt1 = Data Table( aftlst[i] )
),
If( Contains( aftlst[i], "PValues" ),
dt1 = Data Table( aftlst[i] )
)
)
);
mat1 = dt1 << get as matrix;
Code Explanation:
- Open data table;
- Create associative array before analysis.
- Perform summary Y by X analysis.
- Create associative array after analysis.
- Remove unchanged windows from associative array.
- Get keys from associative array.
- Loop through each key.
- Check if host is Windows.
- Check if key contains "PValues".
- Assign data table to dt1 if condition met.
Example 5
Summary: Creates and manipulates various data structures, including associative arrays, matrices, lists, and dates, in JMP.
Code:
PyGet_dt = Open("data_table.jmp");
PyGet_A = Associative Array( {"red", "blue"}, {1, 2} );
PyGet_X = 3.1415927;
PyGet_S = "Report Title";
PyGet_M = [1 2 3, 4 5 6, 7 8 9];
PyGet_L = {1.2, -2, 3, 124};
PyGet_L2 = {1, 2 + 3, [11 22]};
d1 = Open("data_table.jmp");
d2 = Open("data_table.jmp");
PyGet_L3 = {d1, d2};
PyGet_date = Informat( "07/15/2000 22:01:31", "Format Pattern", "<MM>/<DD>/<YYYY> <hh24>:<mm>:<ss>" );
s = Log Capture(
Python Submit(
"\[
import jmp
print( f"PyGet_B: {jmp.globals['PyGet_B']} ")
print( f"PyGet_dt: {jmp.globals['PyGet_dt']} ")
print( f"PyGet_A: {jmp.globals['PyGet_A']} ")
print( f"PyGet_X: {jmp.globals['PyGet_X']} ")
print( f"PyGet_S: {jmp.globals['PyGet_S']} ")
print( f"PyGet_M: {jmp.globals['PyGet_M']} ")
print( f"PyGet_L: {jmp.globals['PyGet_L']} ")
print( f"PyGet_L2: {jmp.globals['PyGet_L2']} ")
print( f"d1: {jmp.globals['d1']} ")
print( f"d2: {jmp.globals['d2']} ")
print( f"PyGet_L3: {jmp.globals['PyGet_L3']} ")
print( f"PyGet_date: {jmp.globals['PyGet_date']} ")
]\"
)
);
Code Explanation:
- Open data table;
- Create associative array PyGet_A.
- Assign value to PyGet_X.
- Assign string to PyGet_S.
- Create matrix PyGet_M.
- Create list PyGet_L.
- Create nested list PyGet_L2.
- Open data table;
- Open data table;
- Create list PyGet_L3 with data tables.
- Convert string to date.
- Capture log of Python script execution.
Example 6
Summary: Runs the creation and assignment of various data structures, including an associative array, matrix, list, and date format conversion.
Code:
PyGet_dt = Open("data_table.jmp");
PyGet_A = Associative Array( {"red", "blue"}, {1, 2} );
PyGet_X = 3.1415927;
PyGet_S = "Report Title";
PyGet_M = [1 2 3, 4 5 6, 7 8 9];
PyGet_L = {1.2, -2, 3, 124};
PyGet_L2 = {1, 2 + 3, [11 22]};
d1 = Open("data_table.jmp");
d2 = Open("data_table.jmp");
PyGet_L3 = {d1, d2};
PyGet_date = Informat( "07/15/2000 22:01:31", "Format Pattern", "<MM>/<DD>/<YYYY> <hh24>:<mm>:<ss>" );
Names Default To Here( 0 );
s = Log Capture(
Python Submit(
"\[
import jmp
print( f"PyGet_B: {jmp.here['PyGet_B']} ")
print( f"PyGet_dt: {jmp.here['PyGet_dt']} ")
print( f"PyGet_A: {jmp.here['PyGet_A']} ")
print( f"PyGet_X: {jmp.here['PyGet_X']} ")
print( f"PyGet_S: {jmp.here['PyGet_S']} ")
print( f"PyGet_M: {jmp.here['PyGet_M']} ")
print( f"PyGet_L: {jmp.here['PyGet_L']} ")
print( f"PyGet_L2: {jmp.here['PyGet_L2']} ")
print( f"d1: {jmp.here['d1']} ")
print( f"d2: {jmp.here['d2']} ")
print( f"PyGet_L3: {jmp.here['PyGet_L3']} ")
print( f"PyGet_date: {jmp.here['PyGet_date']} ")
]\"
)
);
Code Explanation:
- Open data table;
- Create associative array PyGet_A.
- Assign value to PyGet_X.
- Assign string to PyGet_S.
- Define matrix PyGet_M.
- Define list PyGet_L.
- Define complex list PyGet_L2.
- Open data table;
- Open data table;
- Define list PyGet_L3 with datasets.
- Convert date string to PyGet_date.
- Disable name conflict warnings.
- Capture log from Python script.
- Execute Python script to print variables.
Example 7
Summary: Runs data table operations, including opening and closing a file, creating an associative array, assigning values to variables, and capturing Python log output.
Code:
dt = Open("data_table.jmp");
Close( dt );
A = Associative Array( {"red", "blue"}, {1, 2} );
X = 1;
S = "Report Title";
jmp var name = 25;
jmp var name b = 28;
Here:x = 1;
lx = Eval( Here:x );
::y = 2;
z = 3;
s = Log Capture( Python Submit( "print(jmp_var_name)" ) );
s = Log Capture( Python Submit( "print(jmp_var_name_b)" ) );
s = Log Capture( Python Submit( "print(localx)" ) );
s = Log Capture( Python Submit( "print(y)" ) );
Code Explanation:
- Open data table.
- Close the table.
- Create associative array.
- Assign value to X.
- Assign string to S.
- Define global variable.
- Define another global variable.
- Assign value to local variable.
- Evaluate local variable.
- Define global variable y.
- Assign value to z.
- Capture Python log output.
- Capture Python log output again.
- Capture Python log output for local variable.
- Capture Python log output for global variable.
Example 8
Summary: Searches for Google News articles related to selected companies in a JMP data table, prompting users to confirm before proceeding with searches exceeding 10 items.
Code:
Names Default To Here( 1 );
//search google news for selected items in a JMP table
//get list of selected companies into lstTerms for searching
dt = Current Data Table();
col = dt << Get Selected Columns;
col = col[1];
rows = dt << Get Selected Rows;
lstTerms = Associative Array( col[rows] ) << Get Keys;
//check if > 10 items are selected, prompt if so
If( N Items( lstTerms ) <= 10,
proceed = 1,
nw = New Window( "Confirm", <<Modal, Text Box( "> 10 items selected. Are you sure you want to proceed?" ) );
If( nw == {Button( -1 )},
proceed = 0,
proceed = 1
);
);
//loop through each search term and pull up google news
prtA = "https://www.google.com/search?q=";
prtC = "&tbm=nws";
If( proceed == 1,
For Each( {i}, lstTerms,
prtB = Char( Substitute( i, " ", "+" ) );
Web( prtA || prtB || prtC );
)
);
Code Explanation:
- Open data table.
- Get selected columns.
- Select first column.
- Get selected rows.
- Extract selected company names.
- Check if more than 10 items selected.
- Prompt user if more than 10 items.
- Confirm user's choice to proceed.
- Define Google News search URL parts.
- Loop through each company name.
- Replace spaces with plus signs.
- Construct full Google News search URL.
- Open search URL in web browser.
Associative Array using Python Connect
Example 1
Summary: Runs the connection to Python, sets variables, and captures logs for debugging purposes.
Code:
Python Connect();
B = As Boolean( 1 );
dt = Open("data_table.jmp");
Close( dt );
A = Associative Array( {"red", "blue"}, {1, 2} );
X = 1;
S = "Report Title";
jmp var name = 25;
jmp var name b = 28;
Here:x = 1;
lx = Eval( Here:x );
::y = 2;
z = 3;
s = Log Capture( Python Submit( "print(jmp_var_name)" ) );
s = Log Capture( Python Submit( "print(jmp_var_name_b)" ) );
s = Log Capture( Python Submit( "print(localx)" ) );
s = Log Capture( Python Submit( "print(y)" ) );
s = Log Capture( Python Submit( "print(z)" ) );
Code Explanation:
- Connects to Python.
- Sets B to true.
- Opens "data_table.jmp".
- Closes the opened table.
- Creates associative array A.
- Assigns X value 1.
- Sets S to "Report Title".
- Defines jmp var name.
- Defines jmp var name b.
- Prints variables using Python.
Example 2
Summary: Runs Python connectivity and data manipulation tasks, including boolean conversion, data table opening and closing, associative array creation, and variable assignment.
Code:
pythonconn = Python Connect();
B = As Boolean( 1 );
dt = Open("data_table.jmp");
Close( dt );
A = Associative Array( {"red", "blue"}, {1, 2} );
X = 1;
S = "Report Title";
jmp var name = 25;
jmp var name b = 28;
Here:x = 1;
lx = Eval( Here:x );
::y = 2;
z = 3;
s = Log Capture( Python Submit( "print(jmp_var_name)" ) );
s = Log Capture( Python Submit( "print(jmp_var_name_b)" ) );
s = Log Capture( Python Submit( "print(localx)" ) );
s = Log Capture( Python Submit( "print(y)" ) );
s = Log Capture( Python Submit( "print(z)" ) );
Code Explanation:
- Connect to Python.
- Convert 1 to boolean.
- Open data table;
- Close the dataset.
- Create associative array.
- Assign integer value.
- Assign string value.
- Assign integer value to variable.
- Assign another integer value to variable.
- Assign integer value locally.
Associative Array using Run Script
Example 1
Summary: Creates a web report by running Bivariate and Distribution scripts, adding images with varying paths, and indexing the report.
Code:
dt = Open("data_table.jmp");
biv = dt << Run Script( "Bivariate" );
webrpt = New Web Report();
webrpt << Add Report( biv );
imageInfo = Associative Array(
["black_rhino_footprint_thumb1.png" => {"footprint", "path to image (full path)"}, "pi_thumb1.png" => {"pi",
"path to image (relative path)"}, "tile_thumb1.png" => {"tile", "path to image (variable)"}, "progress_thumb1.png" => {"progress",
"File argument (full path)"}, "windmap_thumb1.png" => {"windmap", "File argument (relative path)"}, "civilization_thumb1.png" =>
{"civilization", "File argument (variable)"}]
);
webrpt << Add Image( "$SAMPLE_IMAGES/black rhino footprint.jpg", Title( "footprint" ), Description( "path to image (full path)" ) );
imagePath = "$SAMPLE_IMAGES/tile.jpg";
webrpt << Add Image( imagePath, Title( "tile" ), Description( "path to image (variable)" ) );
webrpt << Add Image( File( "$SAMPLE_IMAGES/progress.gif" ), Title( "progress" ), Description( "File argument (full path)" ) );
dis = dt << Run Script( "Distribution" );
webrpt << Add Report( dis );
webrpt << Index( Title( "Report" ), Description( "New Report" ) );
Close( dt, NoSave );
Code Explanation:
- Open data table.
- Run Bivariate script.
- Create new web report.
- Add Bivariate report to web report.
- Define associative array for images.
- Add image with full path to web report.
- Assign image path variable.
- Add image with variable path to web report.
- Add image with File argument to web report.
- Run Distribution script.
- Add Distribution report to web report.
- Index web report.
- Close data table without saving.
Example 2
Summary: Creates a web report by running Bivariate and Distribution analyses, adding images with variable paths, and configuring path styles in JMP.
Code:
dt = Open("data_table.jmp");
biv = dt << Run Script( "Bivariate" );
webrpt = New Web Report();
webrpt << Add Report( biv );
imageInfo = Associative Array(
["black_rhino_footprint_thumb1.png" => {"footprint", "path to image (full path)"}, "pi_thumb1.png" => {"pi",
"path to image (relative path)"}, "tile_thumb1.png" => {"tile", "path to image (variable)"}, "progress_thumb1.png" => {"progress",
"File argument (full path)"}, "windmap_thumb1.png" => {"windmap", "File argument (relative path)"}, "civilization_thumb1.png" =>
{"civilization", "File argument (variable)"}]
);
webrpt << Add Image( "$SAMPLE_IMAGES/black rhino footprint.jpg", Title( "footprint" ), Description( "path to image (full path)" ) );
imagePath = "$SAMPLE_IMAGES/tile.jpg";
webrpt << Add Image( imagePath, Title( "tile" ), Description( "path to image (variable)" ) );
webrpt << Add Image( File( "$SAMPLE_IMAGES/progress.gif" ), Title( "progress" ), Description( "File argument (full path)" ) );
dis = dt << Run Script( "Distribution" );
webrpt << Add Report( dis );
webrpt << Index( Title( "Report" ), Description( "New Report" ) );
Code Explanation:
- Open data table;
- Run Bivariate analysis.
- Create new web report.
- Add Bivariate report to web report.
- Define image information array.
- Add first image to web report.
- Add second image to web report using variable path.
- Add third image to web report using file argument.
- Run Distribution analysis.
- Add Distribution report to web report.
Associative Array using If
Summary: Fits life by X with censor, utilizing a window-based interface to interactively select and configure censor codes.
Code:
dt = Open("data_table.jmp");
If( Mod( Day Of Week( Today() ), 2 ),
dt:Censor << Delete Property( "Value Labels" )
);
fitlifebyx( Censor( :censor ) );
wn = Window( "Fit Life by X" );
actvals = {};
For( i = 1, i <= wn[Popup Box( 2 )] << get menu count, i++,
actvals[i] = wn[Popup Box( 2 )] << get menu text( i )
);
values = Design Nom( :Censor << get values, <<Levels )[2];
labels = :Censor << get value labels;
If( !Is Empty( labels ),
lst = Arg( labels, 1 );
aa = Associative Array();
For( i = 1, i <= N Items( lst ), i++,
aa[Arg( lst[i], 1 )] = Arg( lst[i], 2 )
);
For( i = 1, i <= N Items( values ), i++,
values[i] = Char( aa[values[i]] )
);
,
For( i = 1, i <= N Items( values ), i++,
values[i] = Char( values[i] )
)
);
prefcode = Arg( Arg( Arg( Get Preferences( FitLifeByX( Censor Code ) ), 1 ), 1 ), 1 );
ccodes = values;
wn << close window;
Preference( FitLifeByX( Censor Code( "2.205" ) ) );
fitlifebyx( Censor( :Censor ) );
wn = Window( "Fit Life by X" );
actvals = {};
For( i = 1, i <= wn[Popup Box( 2 )] << get menu count, i++,
actvals[i] = wn[Popup Box( 2 )] << get menu text( i )
);
values = Design Nom( :Censor << get values, <<Levels )[2];
labels = :Censor << get value labels;
If( !Is Empty( labels ),
lst = Arg( labels, 1 );
aa = Associative Array();
For( i = 1, i <= N Items( lst ), i++,
aa[Arg( lst[i], 1 )] = Arg( lst[i], 2 )
);
For( i = 1, i <= N Items( values ), i++,
values[i] = Char( aa[values[i]] )
);
,
For( i = 1, i <= N Items( values ), i++,
values[i] = Char( values[i] )
)
);
prefcode = Arg( Arg( Arg( Get Preferences( FitLifeByX( Censor Code ) ), 1 ), 1 ), 1 );
ccodes = values;
wn << close window;
Code Explanation:
- Open data table.
- Check if today is odd day.
- Delete property if condition met.
- Fit life by X with censor.
- Get window reference.
- Initialize active values list.
- Loop through popup box items.
- Store menu texts in list.
- Get design nominal values.
- Get value labels.
- Check if labels are empty.
- Create associative array from labels.
- Convert values using associative array.
- Get platform preferences.
- Assign censor codes.
- Close window.
- Set new preference for censor code.
- Repeat fit life by X with censor.
- Get new window reference.
- Initialize new active values list.
- Loop through new popup box items.
- Store new menu texts in list.
- Get new design nominal values.
- Get new value labels.
- Check if new labels are empty.
- Create new associative array from labels.
- Convert new values using associative array.
- Get new platform preferences.
- Assign new censor codes.
- Close window.
Associative Array using Add Rows
Summary: Creates a lookup table by selecting choices from two list boxes and adding corresponding rows to the 'lookuptable'.
Code:
Names Default To Here( 1 );
dt1 = New Table( "Table 1",
Add Rows( 4 ),
New Column( "Choice1", Character, "Nominal", Set Values( {"A", "B", "C", "D"} ) )
);
dt2 = New Table( "Table 2",
Add Rows( 5 ),
New Column( "Choice2", Character, "Nominal", Set Values( {"L", "M", "N", "O", "P"} ) )
);
dtlookup = New Table( "lookuptable",
Add Rows( 0 ),
New Column( "dt1choice", Character, "Nominal" ),
New Column( "dt2choice", Character, "Nominal" )
);
lst1 = Associative Array( dt1:Choice1 ) << get keys;
lst2 = Associative Array( dt2:Choice2 ) << get keys;
nw = New Window( "Chooser",
H List Box(
lb1 = List Box( lst1, max selected( 1 ) ),
lb2 = List Box( lst2, max selected( 1 ) ),
),
Button Box( "Make Match",
dtlookup << add rows(
{dt1choice = (lb1 << get selected)[1], dt2choice = (lb2 << get selected)[1]}
)
)
);
Code Explanation:
- Create new table "Table 1".
- Add 4 rows to "Table 1".
- Add column "Choice1" with values A, B, C, D.
- Create new table "Table 2".
- Add 5 rows to "Table 2".
- Add column "Choice2" with values L, M, N, O, P.
- Create new table "lookuptable".
- Add columns "dt1choice" and "dt2choice".
- Create GUI window "Chooser".
- Add list boxes for "Choice1" and "Choice2" selections.