Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Execute c# functions
#3
To return multiple values from C#, can be used 2 ways:

1. Let C# return array.

Macro Macro3241
Code:
Copy      Help
_s=
;┋Country, United States, United Kingdom, France
;My name is ┃Name┃, My country is ┃Country┃, My age is ┃Age┃, thank you!

CsScript x
x.AddCode("")
ARRAY(str) r=x.Call("Formater.Format" _s)
out "B:"
out r[0]
out "C:"
out r[1]

#ret
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class Formater
{
,static void Main(string[] args)
,{
,,if (args.Length == 0)
,,{
,,,return;
,,}
,,Console.WriteLine(Format(args[0]));
,}
,static readonly Regex reLabel = new Regex("┋([^,]*),(\\s*)", RegexOptions.Compiled);
,static readonly Regex reLabel2 = new Regex(",\\s*", RegexOptions.Compiled);
,static readonly Regex reContent = new Regex("┃([^┃]*)┃", RegexOptions.Compiled);
,static readonly Regex reDelims = new Regex("\\r?\\n", RegexOptions.Compiled);
,public static string[] Format(string input)
,{
,,List<string> B = new List<string>();
,,List<string> C = new List<string>();
,,List<string> CLocal = new List<string>();
,,List<string> keysInContent = new List<string>();//#keys in content
,,Dictionary<string, int> dicLabelCtr = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
,,OrderedDictionary dicLabelLocal4B = new OrderedDictionary();
,,//# label
,,MatchEvaluator sbLabel = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelCtr.ContainsKey(k))
,,,{
,,,,int ctr = ++dicLabelCtr[k];
,,,,return string.Format("┃Y_{0}{1}┃{2}{0}{1}[]", k, ctr, m.Groups[2].Value);
,,,}
,,,else
,,,{
,,,,dicLabelCtr[k] = 1;
,,,,return string.Format("┃Y_{0}┃{1}{0}[]", k, m.Groups[2].Value);
,,,}
,,};
,,//# content
,,MatchEvaluator sbContent = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelLocal4B.Contains(k))
,,,{
,,,,keysInContent.Add(k);
,,,,if (dicLabelCtr.ContainsKey(k))
,,,,{
,,,,,return string.Format("┃Y_{0}{1}┃", k, dicLabelCtr[k] + 1);
,,,,}
,,,,else
,,,,{
,,,,,return string.Format("┃Y_{0}┃", k);
,,,,}
,,,}
,,,else
,,,{
,,,,return string.Format("┃N_{0}┃", k);
,,,}
,,};
,,int stack = 0;
,,foreach (var line in reDelims.Split(input))
,,{
,,,if (stack == 0)
,,,{
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,stack = 1;
,,,,,//# procossing prev section
,,,,,for (int i = 0; i < CLocal.Count; i++)
,,,,,{
,,,,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,,,,}
,,,,,foreach (var key in dicLabelLocal4B.Keys)
,,,,,{
,,,,,,if (!keysInContent.Contains((string)key))
,,,,,,{
,,,,,,,C.Add((string)dicLabelLocal4B[key]);
,,,,,,}
,,,,,,else
,,,,,,{
,,,,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,,,,}
,,,,,}
,,,,,C.AddRange(CLocal);
,,,,,CLocal.Clear();
,,,,,keysInContent.Clear();
,,,,,dicLabelLocal4B.Clear();
,,,,,//# next section
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,,else
,,,{
,,,,//#stack=1
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,stack = 0;
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,}
,,//# procossing prev section
,,for (int i = 0; i < CLocal.Count; i++)
,,{
,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,}
,,foreach (var key in dicLabelLocal4B.Keys)
,,{
,,,if (!keysInContent.Contains((string)key))
,,,{
,,,,C.Add((string)dicLabelLocal4B[key]);
,,,}
,,,else
,,,{
,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,}
,,}
,,C.AddRange(CLocal);
,,CLocal.Clear();
,,keysInContent.Clear();
,,dicLabelLocal4B.Clear();
,,//return result
,,return new string[] { string.Join(Environment.NewLine, B.ToArray()), string.Join(Environment.NewLine, C.ToArray()) };
,}
}

2. Or create C# object and call its functions. Example: in CsScript.Call help click link "create object".

Macro Macro3242
Code:
Copy      Help
_s=
;┋Country, United States, United Kingdom, France
;My name is ┃Name┃, My country is ┃Country┃, My age is ┃Age┃, thank you!

CsScript x
x.AddCode("")
IDispatch o=x.CreateObject("Formater")
str B C
BSTR bB bC
o.Format(_s &bB &bC)
B=bB
C=bC
out "B:"
out B
out "C:"
out C

#ret
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class Formater
{
,static readonly Regex reLabel = new Regex("┋([^,]*),(\\s*)", RegexOptions.Compiled);
,static readonly Regex reLabel2 = new Regex(",\\s*", RegexOptions.Compiled);
,static readonly Regex reContent = new Regex("┃([^┃]*)┃", RegexOptions.Compiled);
,static readonly Regex reDelims = new Regex("\\r?\\n", RegexOptions.Compiled);
,public void Format(string input, ref string rB, ref string rC)
,{
,,List<string> B = new List<string>();
,,List<string> C = new List<string>();
,,List<string> CLocal = new List<string>();
,,List<string> keysInContent = new List<string>();//#keys in content
,,Dictionary<string, int> dicLabelCtr = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
,,OrderedDictionary dicLabelLocal4B = new OrderedDictionary();
,,//# label
,,MatchEvaluator sbLabel = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelCtr.ContainsKey(k))
,,,{
,,,,int ctr = ++dicLabelCtr[k];
,,,,return string.Format("┃Y_{0}{1}┃{2}{0}{1}[]", k, ctr, m.Groups[2].Value);
,,,}
,,,else
,,,{
,,,,dicLabelCtr[k] = 1;
,,,,return string.Format("┃Y_{0}┃{1}{0}[]", k, m.Groups[2].Value);
,,,}
,,};
,,//# content
,,MatchEvaluator sbContent = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelLocal4B.Contains(k))
,,,{
,,,,keysInContent.Add(k);
,,,,if (dicLabelCtr.ContainsKey(k))
,,,,{
,,,,,return string.Format("┃Y_{0}{1}┃", k, dicLabelCtr[k] + 1);
,,,,}
,,,,else
,,,,{
,,,,,return string.Format("┃Y_{0}┃", k);
,,,,}
,,,}
,,,else
,,,{
,,,,return string.Format("┃N_{0}┃", k);
,,,}
,,};
,,int stack = 0;
,,foreach (var line in reDelims.Split(input))
,,{
,,,if (stack == 0)
,,,{
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,stack = 1;
,,,,,//# procossing prev section
,,,,,for (int i = 0; i < CLocal.Count; i++)
,,,,,{
,,,,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,,,,}
,,,,,foreach (var key in dicLabelLocal4B.Keys)
,,,,,{
,,,,,,if (!keysInContent.Contains((string)key))
,,,,,,{
,,,,,,,C.Add((string)dicLabelLocal4B[key]);
,,,,,,}
,,,,,,else
,,,,,,{
,,,,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,,,,}
,,,,,}
,,,,,C.AddRange(CLocal);
,,,,,CLocal.Clear();
,,,,,keysInContent.Clear();
,,,,,dicLabelLocal4B.Clear();
,,,,,//# next section
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,,else
,,,{
,,,,//#stack=1
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,stack = 0;
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,}
,,//# procossing prev section
,,for (int i = 0; i < CLocal.Count; i++)
,,{
,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,}
,,foreach (var key in dicLabelLocal4B.Keys)
,,{
,,,if (!keysInContent.Contains((string)key))
,,,{
,,,,C.Add((string)dicLabelLocal4B[key]);
,,,}
,,,else
,,,{
,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,}
,,}
,,C.AddRange(CLocal);
,,CLocal.Clear();
,,keysInContent.Clear();
,,dicLabelLocal4B.Clear();
,,//return result
,,rB=string.Join(Environment.NewLine, B.ToArray());
,,rC=string.Join(Environment.NewLine, C.ToArray());
,}
}


Messages In This Thread
Execute c# functions - by Davider - 09-01-2022, 11:52 AM
RE: Execute c# functions - by Gintaras - 09-01-2022, 12:42 PM
RE: Execute c# functions - by Gintaras - 09-01-2022, 01:07 PM
RE: Execute c# functions - by Davider - 09-01-2022, 01:17 PM
RE: Execute c# functions - by Davider - 09-05-2022, 10:37 AM
RE: Execute c# functions - by Gintaras - 09-05-2022, 11:53 AM
RE: Execute c# functions - by Davider - 09-05-2022, 10:40 PM
RE: Execute c# functions - by Gintaras - 09-06-2022, 04:00 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)