Regex version:

dolsub =: '\$[A-Z]*'&(".@}. rxapply)

This just uses the current locale.

Henry Rich

On 7/2/2012 11:52 AM, Raul Miller wrote:
> Some programming languages (unix shell, perl, php) provide a $
> substitution mechanism where a literal string may contain embedded
> variable references which get replaced with the value of the variable.
>
> This can be handy.
>
> Here's an example implementation of this kind of mechanism in J:
>
> IDCHARS=: '$ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>
> dolsub1=: 1 :0
>    loc=. boxopen m
>    n=. y (e. i. 0:) IDCHARS
>    if. n < 3 do. <y return. end.
>    nm=. }.n{. y
>    if. 0 ~: nc__loc <nm do. <y return. end.
>    <(,":do__loc nm),n}.y
> )
>
> dolsub=: 1 :0
>    2 }. [: ; [: m dolsub1;.1 '$ ', ]
> )
>
> Example, given:
>
> sample=:0 :0
>
>    This is some sample text
>    Here's an example substitution:
>    ($IDCHARS)
> )
>
> dolsub can replace $IDCHARS with the value defined for IDXCHARS:
>
>     'base' dolsub sample
>
>    This is some sample text
>    Here's an example substitution:
>    ($ABCDEFGHIJKLMNOPQRSTUVWXYZ)
>
>
> Notes:
>
> dolsub is an adverb.  The locale used to resolve names is its left
> argument.  If you do not want the z locale to be referenced, make sure
> it's not a part of the locale's path.
>
> In this implementation, I only allow names which are ALL CAPS and
> which are at least 2 characters long.  Extending this to support lower
> case alphanumerics is trivial.
>
> In this implementation, I only interpolate names with noun definitions.
>
> I do not attempt to provide table formatting for tabular values,
> because that gets into ambiguities I do not care about.  I'm ravelling
> so I can ignore leading 1 dimensions.
>
> The expression y (e. i. 0:) IDCHARS is supported by special code (but
> that's only a minor speed improvement for this kind of routine).
>
> FYI,
>

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to