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;