Notifications
Clear all

[Closed] Fast search through text file

no surprise, in order to iterate using foreach you need to split the doc into lines array
choose something from this SO thread

alternatively you could simply read the doc line by line and do what you want. this must be even faster

using (StringReader sr = new StringReader(text)) {
    string line;
    while ((line = sr.ReadLine()) != null) {
        // do something
    }
}

I execute only the code I posted above and there is an error:

-- Runtime error: .NET runtime exception: Could not load file or assembly 'file:///C:\Users\XXXXXX\AppData\Local\Temp\xi25bkge.dll' or one of its dependencies. The system cannot find the file specified.

If the c# syntax is correct it should build the dll and to load it, and if there is an erorr in the code it has to thrown an error when I use it, or I am wrong?

The thing is that it shouldn’t compile at all unless you commented out everything inside ProcessDocument method.
This one should compile: https://dotnetfiddle.net/7kNr05

add this to your code and see what errors it prints

...
        compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #( source )

		if (compilerResults.Errors.Count > 0 ) then
		(
			local errs = stringstream ""
			for i = 0 to (compilerResults.Errors.Count-1) do
			(
				local err = compilerResults.Errors.Item[i]
				format "Error:% Line:% Column:% %\n" err.ErrorNumber err.Line err.Column err.ErrorText to:errs
			)
			format "%\n" errs
			undefined
		)
		else
		(
			compilerResults.CompiledAssembly.CreateInstance ". . ."			
		)

This is the end of the code:

source += "	}"

		csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
		compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

		compilerParams.ReferencedAssemblies.AddRange #("System.dll")

		compilerParams.GenerateInMemory = on
		compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
		
		if (compilerResults.Errors.Count > 0 ) then
		(
			local errs = stringstream ""
			for i = 0 to (compilerResults.Errors.Count-1) do
			(
				local err = compilerResults.Errors.Item[i]
				format "Error:% Line:% Column:% %\n" err.ErrorNumber err.Line err.Column err.ErrorText to:errs
			)
			format "%\n" errs
			undefined
		)
		else
		(
			compilerResults.CompiledAssembly.CreateInstance ". . ."			
		)
		
		assembly = compilerResults.CompiledAssembly
		assembly.CreateInstance "DocProcessor"
	)

And this is the error message:


StringStream:"Error:CS0116 Line:1 Column:58 A namespace cannot directly contain members such as fields or methods
"
-- Error occurred in anonymous codeblock; filename: C:\Temp\cSharp_Test_01.ms; position: 3890; line: 120
-- Runtime error: .NET runtime exception: Could not load file or assembly 'file:///C:\Users\XXXX\AppData\Local\Temp\c4f51gay.dll' or one of its dependencies. The system cannot find the file specified.
-- MAXScript callstack:
--	thread data: threadID:2704
--	------------------------------------------------------
--	[stack level: 0]
--	In CreateArrAssembly(); filename: C:\Temp\cSharp_Test_01.ms; position: 3891; line: 120
--	member of: anonymous codeblock
--		Locals:
--			compilerParams: dotNetObject:System.CodeDom.Compiler.CompilerParameters
--			compilerResults: dotNetObject:System.CodeDom.Compiler.CompilerResults
--			source: "using System;using Text;using System.Collections.Generic;System.Text.RegularExpressions;	public class DocProcessor	{		List<string> _kappa = new List<string>();		List<string> _omicron = new List<string>();		List<string> _upsilon = new List<string>();		List<int> _kappaIdx = new List<int>();		List<int> _omicronIdx = new List<int>();		List<int> _upsilonIdx = new List<int>();		public string[] kappa {			get			{				return _kappa.ToArray(); 			}		}		public string[] omicron {			get			{				return _omicron.ToArray(); 			}		}		public string[] upsilon {			get			{				return _upsilon.ToArray(); 			}		}		public int[] kappaIdx {			get			{				return _kappaIdx .ToArray(); 			}		}		public int[] omicronIdx  {			get			{				return _omicronIdx .ToArray(); 			}		}		public int[] upsilonIdx  {			get			{				return _upsilonIdx .ToArray(); 			}		}		var reOmicron = new Regex( "^omicron\b", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled );		var reKappa = new Regex( "^kappa\b", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled );		var reUpsilon = new Regex( "^upsilon\b", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled );		var spaces = new char[]{' ','	'};		public void ProcessDocument( string doc )		{			line_index = 0;			for each line in doc			{				line_index++;				lline = line.TrimStart( spaces );				if ( reOmicron.IsMatch( lline ) )				{					_omicron.add( line );					_omicronIdx.add( line_index );				}				else				if ( reKappa.IsMatch( lline ) )				{					_kappa.add( line );					_kappaIdx.add( line_index );				}				else				if ( reUpsilon.IsMatch( lline ) )				{					_upsilon.add( line);					_upsilonIdx.add( line_index );				}			}		}	}"
--			errs: StringStream:"Error:CS0116 Line:1 Column:58 A namespace cannot directly contain members such as fields or methods
"
--			csharpProvider: dotNetObject:Microsoft.CSharp.CSharpCodeProvider
--			assembly: undefined
--		Externals:
--			owner: <CodeBlock:anonymous>
--	------------------------------------------------------
--	[stack level: 1]
--	called from anonymous codeblock; filename: C:\Temp\cSharp_Test_01.ms; position: 3984; line: 124
--		Locals:
--			CreateArrAssembly: CreateArrAssembly()
--		Externals:
--	------------------------------------------------------
--	[stack level: 2]
--	called from top-level

StringStream:”Error:CS0116 Line:1 Column:58 A namespace cannot directly contain members such as fields or methods

you can always read the reason behind the error on msdn
.

source += “System.Text.RegularExpressions;”

should be

source += “using System.Text.RegularExpressions;”

Thank you.
I updated the code as you suggested. Then there was errors that ‘var’ can’t be used so I removed the var from the code, and not I have this

StringStream:"Error:CS1519 Line:1 Column:848 Invalid token '=' in class, struct, or interface member declaration
Error:CS1520 Line:1 Column:854 Method must have a return type
Error:CS1031 Line:1 Column:861 Type expected
Error:CS1519 Line:1 Column:905 Invalid token '|' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:931 Invalid token '|' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:955 Invalid token ')' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:967 Invalid token '=' in class, struct, or interface member declaration
Error:CS1520 Line:1 Column:973 Method must have a return type
Error:CS1031 Line:1 Column:980 Type expected
Error:CS1519 Line:1 Column:1022 Invalid token '|' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:1048 Invalid token '|' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:1072 Invalid token ')' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:1086 Invalid token '=' in class, struct, or interface member declaration
Error:CS1520 Line:1 Column:1092 Method must have a return type
Error:CS1031 Line:1 Column:1099 Type expected
Error:CS1519 Line:1 Column:1143 Invalid token '|' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:1169 Invalid token '|' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:1193 Invalid token ')' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:1204 Invalid token '=' in class, struct, or interface member declaration
Error:CS1519 Line:1 Column:1216 Invalid token '{' in class, struct, or interface member declaration
Error:CS1518 Line:1 Column:1235 Expected class, delegate, enum, interface, or struct
Error:CS1022 Line:1 Column:1712 Type or namespace definition, or end-of-file expected
"

This ‘Column: 1235’ is showing where the error is, right? But how to find this is maxscript editor(if it is possible)?

1 Reply
(@serejah)
Joined: 10 months ago

Posts: 0

just put all of your c# code to dotnetfiddle and try to run, it will show you places where the syntax isn’t correct
and btw, why not using single multiline string in mxs for c# source code? example

upd.
and to answer your question
can’t declare variables like this at the class level, move them into a ProcessDoc method or declare them with the explicit type

image

I’ve posted updated version above, here’s the link: https://dotnetfiddle.net/7kNr05
you don’t really have to split it to collect the data

Thank you.
Can this version work if I sent a string, not a txt file?

The one I posted? Yes, it works only for strings.
If you want to read the text from the file line by line here’s the example from msdn

Page 2 / 10