cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Substitute First Occurrence

It would be quite convenient if JMP allowed the Substitute() function to have an optional argument such as Substitute( ...., First Occurrence( 1 ) ).  This will help when passing arguments into a function by reference.  Consider the following:

some function = Function( {_function, _args},
	{Default Local},
	function = Name Expr( _function );
	Eval( Substitute( _args, {}, Expr( function ) ) )
);

other function = Function( {_1, _2, _3 = "optional"},
	Show( _1, _2, _3 )
);

some function( Name Expr( other function ), {1, 2, 3} );
/* OUTPUT
_1 = 1;
_2 = 2;
_3 = 3;
*/

some function( Name Expr( other function ), {1, {2, 4}, 3} );
/* EXPECTED OUTPUT
_1 = 1;
_2 = {2, 4};
_3 = 3;
*/

Here in the first function I am calling a referenced function with a given argument list.  The problem is that the Subsitute() replaces all lists with the function call, instead of the outer one only.  This is where it would be nice to substitute only the first occurrence of the List() expression.

4 Comments
Status changed to: Acknowledged

Hi @ErraticAttack, thank you for your suggestion! We have captured your request and will take it under consideration.

SamGardner
Level VII
Status changed to: Investigating

@ErraticAttack thanks for the suggestion.  We are looking into this and will follow up on what we decide to do or if there is a workaround to do what you want to do.  

jthi
Super User

Other option could be to have Count() argument which would let user to determine how many occurrences to substitute (was just writing wish list item for this, but I think this covers the most used use case). 

 

 

But I will paste the text I had written below:

 

 

What inspired this wish list request? 

Replacing sequences in a string using complicated regex patterns when I could basically utilize Substitute, if it didn't replace all occurrences.

 

 

What is the improvement you would like to see? 

Provide <Count(c = 0)> named argument to Substitute() and Substitute Into() functions. This argument would let user to determine how many occurrences to replace. By default it could be 0? which would replace all occurrences. This substitution should work with all different types which substitute() works with (expressions, strings, list).

 

This would also somehow take into consideration that you can perform multiple substitutions with Substitute(). There are few options for this

  1. User can set other argument (GLOBALCOUNT?) in which case it will use "total" count for replacements meaning, it will replace as many values as count tells it to. If this was disabled, it would use separate (but still same) count value for each of the replacements
  2. Let user provide a list of counts to substitute
  3. Both

I most likely would prefer option 1 or 3 but JMP developers might have better ideas.

 

Why is this idea important? 

Makes it easier to substitute different things in JSL instead of relying on more complicated solutions (string splicing, regex, rebuilding expressions, ...). 

hogi
Level XII

related wish: 🙏 Expression Indexing: read and write access 

 

The special case of  Replace Head() ? will be the by far most important use case.

So, maybe donT' invest the time for the whole framework - just allow Replace Head() ? via  Substitute( ...., First Occurrence( 1 ) )..