Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple lines of string to multiple parameters
#1
I want to implement a function to convert multiple lines of strings into multiple parameters
I created a member function str.CallCS
Currently, it only supports three lines of text (three parameters), but x.Call supports up to 10 parameters.

My method looks stupid
 
Code:
Copy      Help
,sel as.len
,,case 1 ret x.Call(_Fun as[0])
,,case 2 ret x.Call(_Fun as[0] as[1])
,,case 3 ret x.Call(_Fun as[0] as[1] as[2])
,,;......

Is there a simpler and smarter solution?

PS: This function may not be suitable, but sometimes I need the requirement for multiple unknown numbers of parameters

Thanks in advance for any suggestions and help
david

Macro Test
 
Code:
Copy      Help
_s=
;using System;
;
;public class cla
;{
;,public static string Fun1(string str1)
;,{
;,,string result = str1;
;,,Console.WriteLine(result);
;,,return result;
;,}
;
;,public static string Fun2(string str1, string str2)
;,{
;,,string result = str1 + str2;
;,,Console.WriteLine(result);
;,,return result;
;,}
;
;,public static string Fun3(string str1, string str2, string str3)
;,{
;,,string result = str1 + str2 + str3;
;,,Console.WriteLine(result);
;,,return result;
;,}
;}

;1 arg
str args=
;first parameter
_s.CallCS("Fun1" args)

;2 args
args=
;first parameter
;second parameter
_s.CallCS("Fun2" args)

;3 args
args=
;first parameter
;second parameter
;Third parameter
_s.CallCS("Fun3" args)

Member function str.CallCS
Code:
Copy      Help
function~ str'_Fun [str'_Arg]

CsScript x.AddCode(this)

if _Arg.len
,ARRAY(str) as=_Arg
,sel as.len
,,case 1 ret x.Call(_Fun as[0])
,,case 2 ret x.Call(_Fun as[0] as[1])
,,case 3 ret x.Call(_Fun as[0] as[1] as[2])
else
,ret x.Call(_Fun)
#2
The following methods are tested to be effective, Is there a better way?

Macro Test
Code:
Copy      Help
_s=
;using System;
;
;public class cla
;{
;,public static string Fun1(string str1)
;,{
;,,string result = str1;
;,,Console.WriteLine(result);
;,,return result;
;,}
;
;,public static string Fun2(string str1, string str2)
;,{
;,,string result = str1 + str2;
;,,Console.WriteLine(result);
;,,return result;
;,}
;
;,public static string Fun3(string str1, string str2, string str3)
;,{
;,,string result = str1 + str2 + str3;
;,,Console.WriteLine(result);
;,,return result;
;,}
;}

str arg1=
;first parameter
_s.CallCS("Fun1" arg1)

str arg2=
;second parameter
_s.CallCS("Fun2" arg1 arg2)

str arg3=
;Third parameter
_s.CallCS("Fun3" arg1 arg2 arg3)

Member function str.CallCS
Code:
Copy      Help
function~ str'_Fun [str'_Arg1] [str'_Arg2] [str'_Arg3] [str'_Arg4]

CsScript x.AddCode(this)

sel getopt(nargs)
,case 1 ret x.Call(_Fun)
,case 2 ret x.Call(_Fun _Arg1)
,case 3 ret x.Call(_Fun _Arg1 _Arg2)
,case 4 ret x.Call(_Fun _Arg1 _Arg2 _Arg3)
,case 5 ret x.Call(_Fun _Arg1 _Arg2 _Arg3 _Arg4)
#3
You can do this much simpler.

Member function str.CallCS
 
Code:
Copy      Help
function~ str'_Fun [ARRAY(str)&as]
CsScript x.AddCode(this)

if(as.len)
,ret x.Call(_Fun as)
else
,out "Error!! No arguments were passed"


Function test
Code:
Copy      Help
_s=
;using System;
;
;public class cla
;{
;,public static string Fun1(string[] args)
;,{
;;;;;;;;//use if space is added at end of each line in multiline args str below
;,,//string result = string.Concat(args);
;;;;;;;// use if no space is added at end of each line in multiline args str below
;;;;;;;;string result = string.Join(" ", args);
;,,Console.WriteLine(result);
;,,return result;
;,}
;}

ARRAY(str) a
;1 arg
str args=
;First parameter
a=args
_s.CallCS("Fun1" a)

;2 args
args=
;First parameter
;Second parameter
a=args
_s.CallCS("Fun1" a)

;3 args
args=
;First parameter
;Second parameter
;Third parameter
a=args
_s.CallCS("Fun1" a)

;4 args
args=
;First parameter
;Second parameter
;Third parameter
;Forth parameter
a=args
_s.CallCS("Fun1" a)

;no args error handling
args=""
a=args
_s.CallCS("Fun1" a)
#4
wow, thank you so much


Forum Jump:


Users browsing this thread: 1 Guest(s)