This is leftover thought process from previous thread about moving text lines. The OP gave up on it, but looked like an opportunity for some more CLR research. Below script is kludged from the c# code I left off with on that thread. Script makes sense, but on the line oList.Add(s) I again get the 'non static-method needs a target' error. Makes no sense and all googling shows that a list add() just adds something. Anyway, gave it a shot :-[ and text file attached.
cFile = dirscript():"MV.txt"
cFile1 = dirscript():"MV1.txt"
If ! FileExist(cFile) Then Terminate(@TRUE,"Canceling: File Not Found",cFile)
ObjectClrOption("useany", "System")
ObjectClrOption("useany", "System.IO")
ObjectClrOption("useany", "System.Collections")
oFile = ObjectClrNew("System.IO.File")
oList = ObjectClrNew("System.Collections.IList")
lines = oFile.ReadAllLines(cFile)
Foreach s in lines
oList.Add(s)
Next
cline = oList[4]
oList.RemoveAt(4)
oList.Insert(3, cline)
oFile.WriteAllLines(cFile1, oList.ToArray())
oFile=0
oList=0
Exit
Doesn't really answer your question but you could do something like:
cFile = dirscript():"MV.txt"
cFile1 = dirscript():"MV1.txt"
If ! FileExist(cFile) Then Terminate(@TRUE,"Canceling: File Not Found",cFile)
ObjectClrOption("useany", "System")
ObjectClrOption("useany", "System.IO")
oFile = ObjectClrNew("System.IO.File")
lines = oFile.ReadAllLines(cFile)
cur = lines[4]
lines[4] = lines[3]
lines[3] = cur
oFile.WriteAllLines(cFile1, lines)
oFile=0
Exit
The type "System.Collections.IList" exists both as a generic and an interface. The code you posted in the other thread is using the generic form and generics are not supported by WIL hosting. The interface form requires creating a subclass dirived from the IList interface and you can't implement homegrown classes with WIL hosting without using the Framework's compiler on some inline C#.
Jim's solution seems like a much simpler option.
Quote from: td on October 15, 2020, 02:00:12 PM
Jim's solution seems like a much simpler option.
For sure.
For future reference, there is the "ArrayList" class. Haven't used it but the class is a subclass of iList. Something to keep in mind, I guess.
Quote from: td on October 15, 2020, 05:25:06 PM
For future reference, there is the "ArrayList" class. Haven't used it but the class is a subclass of iList. Something to keep in mind, I guess.
Thanks. Collections are fascinating.
I was expecting to have someone slip in with "you could do all this with WB array functions" - arrayfileget() etc... Something WB has the CLR is missing is arraysearch() which will return the index. Would make for an interesting script for
- move the line containing "Libertarian"
- before the line containing "Are you" ;D
Probably would be easier with the Item...() functions. Definitely no harder.
Jim
Quote from: JTaylor on October 16, 2020, 07:24:53 AM
Probably would be easier with the Item...() functions. Definitely no harder.
Jim
Yes, but unless overlaid in classes like System.IO.File an index property does not exist. I've seen code where LINQ assemblies are introduced to make searchable lists but not worth the effort with my free time now.
[OFF-TOPIC]
I have an actual ask at work to modify header columns for ^ delimited ascii files being produced from multi-tab .xls files [you know, where the number of rows might be a million, but .xls can only support 65k rows so the output is a multi-tabbed .xls]
I will be using Powershell as their Get-Content returns an array like ReadAllLines()... simple on-liner.
Less useful in really large files but there is an index :)
cFile = dirscript():"MV.txt"
cFile1 = dirscript():"MV1.txt"
If ! FileExist(cFile) Then Terminate(@TRUE,"Canceling: File Not Found",cFile)
lines = FileGet(cFile)
cur = ItemExtract(4,lines,@LF)
lines = ItemRemove(4,lines,@LF)
lines = ItemInsert(cur,4,lines,@LF)
FilePut(cFile1, lines)
I meant there was not an index property in the .net class. This could have all been accomplished with WB internal w/out CLR. I can see only limited reasons to consider a line move in the first place on a file of any size. Personally, I feel better knowing more about 'Non-static method requires a target' - at least until the next Presidential debate. And I hope they don't go into questions about masks - too much mask-debating for me.
Yep. About time for people to come up with a new idea for some type of insane response. The current ones are getting old.
Jim
It also pays to dig into the WB functions... :o
cFile = dirscript():"MV.txt"
cFile1 = dirscript():"MV1.txt"
If ! FileExist(cFile) Then Terminate(@TRUE,"Canceling: File Not Found",cFile)
lines = ArrayFileGet(cFile)
ArraySwapElements(lines, 4, 0, 0, 0, 0, 3, 0, 0, 0, 0 )
ArrayFilePut(cFile1, lines)
Message("Moved",FileGet(cFile):@LF:@LF:FileGet(cFile1))
It appears that we need to the "ArraySwapElements" to the Forum's syntax coloring PHP script.
Hmmm. Doesn't it seem funny that my topics end up getting so many comments/posts on them? So much so, that separate posts need to be started? Not only "Moving lines in a text file", but also "Scraping a website", "SQL Lite" and other posts... I didn't give up on it, as suggested... I just got tired of all the complaining that not enough information was provided.
I think contributors need to really evaluate what's being asked BEFORE making assumptions that not enough information was provided. Enough info was provided to generate a separate post! It appears my posts do generate thought and debate... I guess that's the definition of a forum:
a medium (such as a newspaper or online service) of open discussion or expression of ideas
I'm through patting myself on the back... Thanks for all your posts, comments and help...
Quote from: oradba4u on October 26, 2020, 09:11:59 PM
I'm through patting myself on the back...
It is always nice to distinguish between a contributor and a do-nothing.