cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
BHarris
Level VII

Quickly show Data View with subset of rows that match regex

I'm looking for a JSL script that does the following:

 

  1. Gets the currently selected columns in the active data table.

  2. Takes the first selected column.

  3. Prompts the user to enter a regular expression.

  4. Selects only the rows in the table where the value in that column matches the regex.

  5. Opens a Data View window showing only those matching rows.

 

Is this possible with JSL?

10 REPLIES 10
jthi
Super User

Re: Quickly show Data View with subset of rows that match regex

Looks good! I would make few small changes:

  • Before opening the New Window check that there is a table open and if there isn't one, run stop();
  • If there are no matches, the last row will throw an error. You could either check selected row count and use stop() if it is 0 or show information to the user that there was no matches
  • << Get Rows Where is mos likely faster than << Select Where
    • Some additional speed-up can most likely be gained by evaluating the column reference
  • << On Validate could also be used if you wish to let user input new regex if the first doesn't have any matches

 

View more...
Names Default To Here(1); 

If(N Table() == 0,
	stop(); // display error message to user if necessary
);

dt = Current Data Table();

nw = New Window("Enter search regex",
	<<modal,
	<<return result,
	V List Box(align("center"),
		Panel Box("Input search regex", 
			userinput = Text Edit Box("", 
				<<Set Script(
					okBtn << Click()
				)
				, <<set width(400)
				, << Set Hint("search regex...")
			)
		),
		Lineup Box(N Col(2),
			okBtn = Button Box("OK"),
			Button Box("Cancel")
		)
	)
);

If(nw["Button"] != 1,
	stop();
);

inputRegex = nw["userInput"];
If(IsMissing(inputRegex),
	stop(); // empty regex
);

selCols = dt << Get Selected Columns("String");
If(N Items(selCols) > 0,
	col = selCols[1];
,
	col = Column(dt, 1) << get name;
);

selrows = Eval(EvalExpr(
	dt << Get Rows Where(!Is Missing(Regex(Expr(NameExpr(As Column(dt, col))), inputRegex, "1", IGNORECASE)));
));

If(N Rows(selrows) == 0,
	stop(); // display error message if necessary, this could also be done already within modal window (<< On Validate), if you wish to let user perform new search
);

dt2 = dt << Subset(Rows(selrows), Selected Columns(0), Link to original data table(1), Output Table(""));
// dt2 << Select Rows(1::NRows(dt2)); // to select rows if necessary
-Jarmo

Recommended Articles