cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
lwx228
Level VIII

I can't even do this simple replace!

Not found in the script index, please finish with JSL.Thank you very much!

 

2018-09-04_18-28-59.png

14 REPLIES 14
ian_jmp
Staff

Re: I can't even do this simple replace!

OK. So maybe take a look at regular expressions.

lwx228
Level VIII

Re: I can't even do this simple replace!

I see. That's all I can do.Thankyou!
txnelson
Super User

Re: I can't even do this simple replace!

That is not all the ways it  can be coded in JSL to accomplish what you want, but Ian's suggestion is what I would have also suggested as the solution.

Jim
lwx228
Level VIII

Re: I can't even do this simple replace!

Thanks for your help.
I want to realize that VBA is done with one click In JMP: you can do many complicated operations with just one click of the home-made button.
gzmorgan0
Super User (Alumni)

Re: I can't even do this simple replace!

Iwx228 brings up an interesting wish list item, albeit the example provided is not the typical JSL use case, where there are user selected rows.

 

Excel VBA has a spreadsheet method (function) to do a global replace.  JMP's Substitute() and Substitute Into() functions would meet this users needs without the overhead of regular expressions, since there was no requirement for a pattern, just global replacement. All that is missing is to get and apply it to the current selections. The logical syntax would be

 dt << Replace( searchStr, replaceStr, arguments for selections either select rows and selected cols or all, or.....)

 

Below is a prototype script called ReplaceCell() with usage examples.

A few items to note:

  • Applies to the current data. Aborts if no current data table.
  • Aborts if no selected columns. If no selected rows, all rows in the selected columns are used. 
  • As written this function only applies to Character columns; it can be extended to Numeric columns, by using char() and num() functions.
  • Replaces by row, so looping happens once.
  • This could have been done by column fairly quicky with LFunc() which I posted previously, but wanted to keep ths simple.
  • Use Include() for JSL. No need to make it an Add-In since CTRL + F already does this

  

ReplaceCell = Function({src,rpl},{cdt, scol, srow, i, j, val},
if(ntable()>0, cdt=current data table(), throw("no table available"));
scol = cdt << get selected columns;
if(nitems(scol) ==0, throw("no columns are selected...aborting"));
srow = cdt << get selected rows();
if (nrow(srow)==0, srow = Transpose(1::nrow(cdt)) ); //if no rows are selected, assume the entire column.
for(i=1, i<=nrow(srow), i++,
for(j=1, j<=nitems(scol), j++,
val = scol[j][srow[i]];
if( IsString(val), scol[j][srow[i]] = Substitute(val, src, rpl) )
);
)//end for i

); //end function

//test cases create a new table with 2 selected columns
ndt = New Table( "Test 1",
Add Rows( 10 ),
New Column( "A",
Character,
"Nominal",
Set Values(
{"A7", "B7", "C7", "D7", "E7", "F7", "G7", "A9", "A79", "A707"}
)
),
New Column( "B",
Character,
"Nominal",
Set Values(
{"M7", "M77", "M07", "MX7", "E7", "E2", "E3", "F9", "G27", "M99"}
)
)
);

//replace all 7's with 1's
ReplaceCell("7","1"); //aborts no selected columns

ndt:A << set selected(1);
ndt:B << set selected(1);

//replace all 7's with 1's
ReplaceCell("7","1");
ndt << bring window to front;

//now try it with selected rows

ndt << select rows([1,2,4,7,9,10]);

ReplaceCell("1","6");
ndt << bring window to front;