Can't see what is missing

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

Moderators: Tech Support, phpbb_admin

Can't see what is missing

Postby Ajay Askoolum » October 23rd, 2013, 4:17 pm

Code: Select all
         '∆we' ⎕cse 'Exec' (,[⍬]'String.Format(@"{0} this is my {1} string {2} recycle {0} ","ajay", "new","900.23");      ')
¯1
      '∆we' ⎕cse 'ExecStmt' (,[⍬]'String.Format(@"{0} this is my {1} string {2} recycle {0} ","ajay", "new","900.23");      ')
CSE ERROR: An invalid argument was entered
      '∆we' ⎕cse 'ExecStmt' (,[⍬]'String.Format(@"{0} this is my {1} string {2} recycle {0} ","ajay", "new","900.23");      ') 
            ^


How do I make this statement return the result of the evaluation?
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: Can't see what is missing

Postby joe_blaze » October 23rd, 2013, 5:14 pm

Hi Ajay:

Thanks for your examples, I'm sure that others will learn from them too.

Let's examine your specific example:
I recommend that you assign (,[⍬]'String.Format(@"{0} this is my {1} string {2} recycle {0} ","ajay", "new","900.23"); ') to an APL+WIn variable and check its shape, which is 90 1.

Problems with your use of the CSE 'Exec' method:
Since your argument is an APL+Win text array with 90 rows and only one column, it cannot represent a valid C# executable statement.
Having only one column means that the C# keywords, e.g. "String" "Format" are split on different rows and that is not valid C#.
Being a 2-dimensional APL+Win text array your argument is a valid argument to the []CSE 'Exec' method so APL+Win does not throw an exception
However since your argument does not represent valid C# executable statements, the Microsoft C# debugger/compiler does throw an exception
The APL+Win []CSE system function indicates this with the -1 result.
When you receive a -1 result, you should use the CSE 'GetLastError' method to see why the Microsoft C# debugger/compiler is complaining,
If you do that you will see that the C# exception message is '... ; expected'. That is a reasonable error when a key word is split.

Problems with your use of the CSE 'ExecStmt' method
Since your argument a APL+Win text array (with 90 rows and 1 column) and not a text vector, it is not a conformable argument to the CSE 'ExecStmt' method.
The 'ExecStmt' method requires an APL+Win text vector argument.
In this situation, the APL+Win []CSE system function throws an APL+Win exception.

Now let's try to get the CSE to do what you want.

The CSE 'Exec', 'ExecStmt' and 'ExecFile' methods are not designed to return values to APL+Win from the associated C# instance of the CSE.
These methods act upon the C# instance of the CSE and report the efficacy of the C# executable statements in their arguments.
To obtain values of C# variables, properties or method results, use the CSE 'GetValue' method.

So to modify your example a bit:

⎕cself←'C'⎕cse 'Init' 'System'
⎕cse 'ExecStmt' 'using System;'
0
⎕pw←120
S←'String.Format(@"{0} this is my {1} string {2} recycle {0} ","ajay", "new","900.23");'
⎕cse 'ExecStmt' S
0
⍝ ↑Executed correctly in C#, but nothing was assigned on the C# side
⍝ So using 'ExecStmt' is not going to do what you want.

⎕cse 'GetValue' S
ajay this is my new string 900.23 recycle ajay

⍝ ↑Using the CSE 'GetValue' method does the job!
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: Can't see what is missing

Postby Ajay Askoolum » October 23rd, 2013, 6:42 pm

Thanks Joe. By the time I read your interesting response, I had been through the same thought processes as in your answer up to this line:

Code: Select all
⎕pw←120


From then on, you have provided a dramatic insight (for me) into []cse. Consider the following:

Code: Select all
    ∇ String
[1]
[2]   Z←'∆we' ⎕cse 'Init' ('System')
[3]   script←⊂''
[4]   script←script,⊂'using System;'
[5]   script←script,⊂'static string stringFormat(string thisString,object[] fillers)'
[6]   script←script,⊂'        {'
[7]   script←script,⊂'            return String.Format(thisString, fillers[0], fillers[1], fillers[2].To
      String());'
[8]   script←script,⊂'        }'
[9]   Z←'∆we' ⎕cse 'Exec' (⎕←⊃script)
[10]
[11]  →0
[12]        '∆we' ⎕cse 'ExecStmt' 'string a = String.Format(@"{0} this is my {1} string {2} recycle {0}
      ","ajay", "new","900.23");'
[13]  0
[14]        '∆we' ⎕cse 'GetValue' 'a'
[15]
    ∇


I have more or less the same solution the key difference being that you have used a workspace variable (s) and I have used a managed code variable (a, on line 12). Your approach opens up a few possibilities - more on this in due course.

For now, I'd be grateful for a code snippet that calls the function stringFormat from APL+Win (I have tried several scenarios to no avail). Thanks.
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: Can't see what is missing

Postby joe_blaze » October 27th, 2013, 6:38 am

Ajay asks:
... I'd be grateful for a code snippet that calls the .Net String.Format() from APL+Win ...

Joe asks:
Is this what you want to do?

⎕cself←'C' ⎕cse 'Init' 'System'
⎕cse 'ExecStmt' 'using System;'
0
⎕cse 'GetValue' 'String.Format(@"{0} this is my {1} string {2} recycle {0}","ajay", "new", "900.23")'
ajay this is my new string 900.23 recycle ajay
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: Can't see what is missing

Postby joe_blaze » May 22nd, 2014, 11:31 am

You may also be interested in the new 'Scalar value type substitution of APL+Win values' feature in the version of the CSE which is included with APL+Win v14.1.01.
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 19 guests

cron