Need a tip

General discussions related to using the C# Script Engine in APL+Win.

Moderators: Tech Support, phpbb_admin

Need a tip

Postby Ajay Askoolum » July 19th, 2015, 5:06 pm

Consider this example:
Code: Select all
      phrase←"{0} {1} {2} {3}"
      Values←"this" "is" "an" "example"
      ⎕cse 'SetValue' 'phrase' phrase
0
      ⎕cse 'SetValue' 'args' Values
0
      ⎕cse 'GetValue' 'string.Format(phrase,args)'
this is an example

How can I do this without assigning the two variables phrase and args?

I've tried several ways but failed. Consider
Code: Select all
      ⎕cse 'GetValue' 'string.Format({0},{1})',"WWWE {0}" "123,8999"
WWWE 123,8999

{0} = WWWE {0}
{1}= 123,8999
Why is the result WWWE 123,8999 and not WWWE{0} 123,8999?
Ajay Askoolum
 
Posts: 656
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

CSE and C# Value Substitution are separate operations!

Postby joe_blaze » July 23rd, 2015, 4:55 pm

Hi Ajay,

You executed Statement #2, see below, and the CSE is processing this statement correctly. Note that in Statement #2 the values to be substituted are APL+Win values.
The result you expected can be obtained by using Statement #1, see below, and the CSE also processes this statement correctly. Note that in Statement #1 the values to be substituted are C# values.

⎕cself←'C'⎕cse 'Init' 'System'
⎕cse 'GetValue' 'string.Format("{0},{1}","WWWE {0}", "123,8999");' ⍝Statement #1

WWWE {0},123,8999


⎕cse 'GetValue' 'string.Format({0},{1})' "WWWE {0}" "123,8999" ⍝Statement #2

WWWE 123,8999
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

CSE String Value Substitution

Postby joe_blaze » July 24th, 2015, 1:43 am

Hi Ajay,

Here is additional information about the execution of Statement #2.
Statement #2: ⎕cse 'GetValue' 'string.Format({0},{1})' "WWWE {0}" "123,8999"

Step #1: The CSE parses the programmer-provided C# executable statement, i.e. 'string.Format({0},{1}' and determines that there are substitution placeholders in the executable statement string and there are available APL+Win values available to substitute, i.e. "WWWE {0}" and "123,8999". This means that prior to submitting the executable statement to the C# complier, the CSE will perform the substitutions of APL+Win values into the executable statement string.
Step #2: The CSE performs the substitutions into the thusfar-unexecuted, executable statement so that the executable statement is now 'string.Format("WWWE {0}","123,8999")'.
Step #3: The CSE now submits the executable statement, i.e. 'string.Format("WWWE {0}","123,8999")' to the C# compiler.
Step #4: The C# compiler executes the executable statement resulting in "WWWE 123,8999".

The important point here is that to the CSE 'string.Format({0},{1})' is treated as any other text string with respect to the CSE string value substitution technology. Only after the CSE has performed the programmer-requested string value substitution, is that 'substituted' text string submitted to the C# compiler for execution. In all cases the CSE does not parse the programmer-provided executable statement in the manner of C# to try and determine how C# would compile and execute the statement, because to do so the CSE would have to duplicate all of C# and that is not practical or desirable.

As designed and documented, the CSE string value substitution is performing the substitutions on the executable statement before the CSE requests its execution by the C# compiler.
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: Need a tip

Postby Ajay Askoolum » July 25th, 2015, 3:27 am

I don't think the behaviour is consistent:
Code: Select all
      ⎕cse 'GetValue' 'string.Format("Dear {0} {1}")' 100 200
Dear 100 200

      ⎕cse 'GetValue' 'string.Format("Dear {0} {1}")' 'Mr' 'Askoolum'
CSE ERROR: (1,22): error CS1003: Syntax error, ',' expected
      ^
Works fine when the infill values are numeric; fails when they are literals. If specified ouside, the first argument (the phrase) will always be a literal.

(In C#) The second argument of string.Format can be an object, that is, all the infill values specified as an object[]. Should []cse allow nested vectors for the infill values?

For example:
Code: Select all
      phrase←"Dear {0} {1}"
      values←'Mr' 'Askoolum'
      phrase String_Format values
Dear Mr Askoolum

    ∇ Z←L String_Format R
[1]   ⍝ Ajay Askoolum
[2]   →(∣⎕cse 'SetValue' 'phrase' L)↑Error,Z←⎕si[⎕io;]
[3]   →(∣⎕cse 'SetValue' 'args' (R,⊂''))↑Error,Z←⎕si[⎕io;]
[4]   →0,0/Z←⎕cse 'GetValue' 'string.Format(phrase,args);'
[5]   Error:Z,⎕tcnl,⎕cse 'GetLastError'
[6]   Z←''
    ∇
My objective is to elicit the result without creating the variables phrase and values in C#.
Ajay Askoolum
 
Posts: 656
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: Need a tip

Postby joe_blaze » July 28th, 2015, 2:19 am

Hi Ajay,

What version of APL+Win and what version of the CSE are you using?

I am using APL+Win v15.0.01 and CSE v1.1.30.0 and the statement ⎕cse 'GetValue' 'string.Format("Dear {0} {1}")' 100 200 throws an exception CSE ERROR: Object reference not set to an instance of an object

Actually there is no need for 'string.Format()', i.e. something like this is sufficient: ⎕cse 'GetValue' '"ABCD {0} {1}"' 100 200 is sufficient and will execute. However ⎕cse 'GetValue' '"Dear {0} {1}"' 'Mr.' 'Askoolum' throws and exception CSE ERROR: (1,8): error CS1002: ; expected

It will take a bit of investigation to determine why the exceptions are thrown.

Supporting object[] as replacement values is not supported in the current version and will probably not be supported in the next version, but it is a consideration in the future.
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: Need a tip

Postby Ajay Askoolum » July 28th, 2015, 1:36 pm

The versions are:
Code: Select all
      ⎕sysver
14.1.01  May 16 2014 11:12:16  Win/32
      ⎕cse 'version'
1.0.108.0
Ajay Askoolum
 
Posts: 656
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: Need a tip

Postby joe_blaze » July 28th, 2015, 9:35 pm

Hi Ajay,

Thank you for that information. We are planning a new CSE version which should enhance the string value substitution feature of the CSE.
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: Need a tip

Postby Ajay Askoolum » July 29th, 2015, 1:07 am

Hi Joe, Thank you for the update. Very pleased that you intend to enhance the string substitution features of []cse. There are two angles to this.

Firstly, it is very handy to be able to pass values from APL+Win. Let me explain: it is messy to construct
[]cse 'GetValue' 'AddInts(1,2)'

the expression []cse 'GetValue' 'AddInts()' 1 2

is much easier (and natural, almost the way parameters are filled with []wi COM).

Secondly, string.Format is very powerful when you use the options: for example,

string.Format("{0:c}",amount) // adds the local currency symbol
or
string.Format("{0:X2}",value). // Converts value to 2 byte hex, uppercase A=F
etc.

string.Format is globalisation aware (very valuable with APL which isn't) and can be very handy for output formatting, allowing control over conversions, width, etc. You can also use any given value for two output formats: a datetime value can be formatted for date and time

e.g. string.Format("Updated on {0:dateFormat} at {0:timeFormat}",datetimeValue)"

Look forward to the next version.
Ajay Askoolum
 
Posts: 656
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: Need a tip

Postby joe_blaze » July 30th, 2015, 2:23 am

Hi Ajay,

The current CSE version supports 'extended' format specifications when the values to substitute are numeric.
For example:

⎕cse 'GetValue' '"{0:c} {1:c}"' 123 456.2
$123.00 $456.20
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361


Return to APL+Win & The C# Script Engine

Who is online

Users browsing this forum: No registered users and 1 guest

cron