Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
I could do something like... var %format_string = You are $iif(_ $+ [username] == _, not logged in, logged in as [username]) $+ .
var %reply = $eval($replacex(%format_string, [username], %username), 2) This will always work. The only problem is, I would have to document that it MUST be done this way to avoid errors (If I were the only person using this script, it would not be a problem, but this is going to be available to the public, and I don't want to require them to do this). The following alias should do what you need: alias interpolate {
var %s
noop $regsub($1,/\\([a-z\d])/gi,\\ $!+ \1,%s)
return $regsubex($2,/(.*)/, [ $replace(%s,[username],\1) ] )
} The following example illustrates its usage: //var -s %username = TEST, %format_string = $eval(You are $iif([username] == $null,not logged in.,logged in as [username].),0) | echo -a $interpolate(%format_string,%username) However this method, although simple, is limited, in that you can't extend it to support more markers, eg [username] and [password] at the same time; for this you'd probably need an entirely different (and more lengthy/complex) approach. Another, rather minor, limitation is that the user will not be able to use $regsubex itself properly in the format string. I don't know how this part is going to be used, so I'll just warn you about one thing: allowing your script to evaluate a %format_string coming from remote users opens up a huge security hole, for example if a user includes $findfile in it. If it's only going to be used locally though (eg a user specifying a format string in the script's settings GUI for outgoing messages) this isn't an issue. Edit: here's the extended version, turns out it didn't have to be all that different:alias interpolate2 {
if $0 < 3 || $0 !& 1 {
echo -setic info $!interpolate2: invalid parameters
halt
}
var %i = 2, %vars, %sub, %pattern, %2
noop $regsub($1,/\\([a-z\d])/gi,\\ $!+ \1,%sub)
while %i < $0 {
%sub = $replace(%sub,$eval($ $+ %i,2),\ $+ $calc(%i / 2))
%2 = $eval($ $+ $calc(%i + 1),2)
%vars = %vars $+ %2
%pattern = %pattern $+ (.{ $+ $len(%2) $+ })
inc %i 2
}
return $regsubex(%vars,%pattern, [ %sub ] )
} Usage is $interpolate2(format string,marker1,var1,marker2,var2,...), for example
$interpolate2(%format_string,[username],%username,[password],%password,[realname],%realname)
The limitation here is that the combined length of the values of all specified variables cannot exceed ~940 characters
An alternative and simpler way would be:
...
var %s, %markers = username|password|...
noop $regsub(%format_string,/([^ ])\[( %markers )\]/gix,\1 $!+ [\2],%s) $&
$regsub(%s,/\[( %markers )\]([^ ])/gix,[\1] $!+ \2,%s) $&
$regsub(%s,/\[( %markers )\]/gix,% $+ \1,%s)
var %reply = $eval(%s,2)
... This assumes that your variables are named after the markers, eg %username for [username] etc. All you need to do here is edit the %markers variable. This method uses 3 calls to $regsub (as opposed to 2 before) but does not suffer from any of the aforementioned limitations and any performance difference should be negligible.
Last edited by qwerty; 16/02/08 02:43 AM.
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
|