viewpoint-particle

Author Topic: Time to Get 'schooled' again  (Read 100 times)

stanl

  • Pundit
  • *****
  • Posts: 1250
Time to Get 'schooled' again
« on: October 15, 2020, 12:46:06 pm »
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.
Code: Winbatch

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
 

JTaylor

  • Pundit
  • *****
  • Posts: 1397
    • Data & Stuff Inc.
Re: Time to Get 'schooled' again
« Reply #1 on: October 15, 2020, 01:50:06 pm »
Doesn't really answer your question but you could do something like:


Code: Winbatch

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
 

 

td

  • Tech Support
  • *****
  • Posts: 3494
    • WinBatch
Re: Time to Get 'schooled' again
« Reply #2 on: October 15, 2020, 02:00:12 pm »
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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1250
Re: Time to Get 'schooled' again
« Reply #3 on: October 15, 2020, 04:04:48 pm »
Jim's solution seems like a much simpler option.


For sure.

td

  • Tech Support
  • *****
  • Posts: 3494
    • WinBatch
Re: Time to Get 'schooled' again
« Reply #4 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1250
Re: Time to Get 'schooled' again
« Reply #5 on: October 16, 2020, 03:31:06 am »
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.

stanl

  • Pundit
  • *****
  • Posts: 1250
Re: Time to Get 'schooled' again
« Reply #6 on: October 16, 2020, 06:43:33 am »
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

JTaylor

  • Pundit
  • *****
  • Posts: 1397
    • Data & Stuff Inc.
Re: Time to Get 'schooled' again
« Reply #7 on: October 16, 2020, 07:24:53 am »
Probably would be easier with the Item...() functions.   Definitely no harder.   

Jim

stanl

  • Pundit
  • *****
  • Posts: 1250
Re: Time to Get 'schooled' again
« Reply #8 on: October 16, 2020, 08:29:37 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.

JTaylor

  • Pundit
  • *****
  • Posts: 1397
    • Data & Stuff Inc.
Re: Time to Get 'schooled' again
« Reply #9 on: October 16, 2020, 11:06:44 am »
Less useful in really large files but there is an index  :)

Code: Winbatch

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)

 
 

stanl

  • Pundit
  • *****
  • Posts: 1250
Re: Time to Get 'schooled' again
« Reply #10 on: October 16, 2020, 12:11:12 pm »
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.

JTaylor

  • Pundit
  • *****
  • Posts: 1397
    • Data & Stuff Inc.
Re: Time to Get 'schooled' again
« Reply #11 on: October 16, 2020, 02:07:34 pm »
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

stanl

  • Pundit
  • *****
  • Posts: 1250
Re: Time to Get 'schooled' again
« Reply #12 on: October 17, 2020, 05:46:45 am »
It also pays to dig into the WB functions... :o
Code: Winbatch

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))
 

td

  • Tech Support
  • *****
  • Posts: 3494
    • WinBatch
Re: Time to Get 'schooled' again
« Reply #13 on: October 19, 2020, 08:05:13 am »
It appears that we need to the "ArraySwapElements" to the Forum's syntax coloring PHP script.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade