Page 1 of 1

Delete line 1 to n of a file

Posted: Sat Jan 28, 2023 6:36 am
by bbalmerTotalFluency
Hi:

I want to delete the first n lines of a text file.

I can do it by put empty into line 1 to n of URL blah blah blah - which works.

But I've seen online around 2012 this more elegant / clear alternative

delete line 1 to n of URL etc

yet it won't work for me. The file has enough lines. The line above seems very "livecode" simple, elegant, clear.

Does anyone know WHY I can't use it or what I have to do to use it or something like it? I know that I can copy the file into a variable delete the lines and put the variable back, but I'm seeking to be more elegant than that.

The help of the wise and learned - much appreciated on this one.

Re: Delete line 1 to n of a file

Posted: Sat Jan 28, 2023 11:14 am
by Klaus
Obviously DELETING is not supported by Lc with the open file... and url("file:"...) syntax.
So you could file an enhancement request here: https://quality.livecode.com

In the meantime you'll have to use what you wrote:
I know that I can copy the file into a variable delete the lines and put the variable back
BTW: This is exactly what LC does internally when using the url("file:"...) syntax.

Re: Delete line 1 to n of a file

Posted: Sat Jan 28, 2023 1:24 pm
by richmond62
I would import the text into a field, delete the lines, and then write it back to the file.

Re: Delete line 1 to n of a file

Posted: Sat Jan 28, 2023 1:40 pm
by Klaus
We guessed, Richmond, we guessed. :D

Re: Delete line 1 to n of a file

Posted: Sat Jan 28, 2023 1:47 pm
by SparkOut
If you really want the LiveCode engine to do the lifting you can

Code: Select all

put line (n + 1) to -1 of url("file:" & tFile) into url("file:" & tFile)
(In *all* the cases, you would need to make sure that the file in question is not already opened, either in LiveCode or any other app/editor.)

I'd advise using a better variable name than n though.

Re: Delete line 1 to n of a file

Posted: Sat Jan 28, 2023 1:58 pm
by Klaus
SparkOut wrote:
Sat Jan 28, 2023 1:47 pm
If you really want the LiveCode engine to do the lifting you can

Code: Select all

put line (n + 1) to -1 of url("file:" & tFile) into url("file:" & tFile)
(In *all* the cases, you would need to make sure that the file in question is not already opened, either in LiveCode or any other app/editor.)

I'd advise using a better variable name than n though.
Very clever, Sir! :-)

Re: Delete line 1 to n of a file

Posted: Sat Jan 28, 2023 2:09 pm
by SparkOut
well I see that it's not essentially different from the OP's original solution of

Code: Select all

put empty into line 1 to n of URL...
although I think you would have to cater for line endings and account for a leading CR in the resulting file. (ie there's a blank first line)

Re: Delete line 1 to n of a file

Posted: Sat Jan 28, 2023 10:26 pm
by richmond62
Klaus: if you can do something very, very easily (in the way I suggested) why spend donkey's ages on a more complicated way?

Re: Delete line 1 to n of a file

Posted: Sun Jan 29, 2023 10:33 am
by Klaus
Well, using a variable instead of a field is a tiny bit less complicated, because you do not need to create a field. :-D

Re: Delete line 1 to n of a file

Posted: Sun Jan 29, 2023 6:38 pm
by jacque
Klaus wrote:
Sun Jan 29, 2023 10:33 am
Well, using a variable instead of a field is a tiny bit less complicated, because you do not need to create a field. :-D
And working with field text is just about the slowest thing you can do in LC.

Re: Delete line 1 to n of a file

Posted: Sun Jan 29, 2023 7:25 pm
by richmond62
Yerss, well not all of are worried about shaving off a few nanoseconds. 8)

Re: Delete line 1 to n of a file

Posted: Sun Jan 29, 2023 11:27 pm
by FourthWorld
richmond62 wrote:
Sun Jan 29, 2023 7:25 pm
Yerss, well not all of are worried about shaving off a few nanoseconds. 8)
But newcomers may wonder why the extra step is added to the instruction when it adds nothing to the operation.

Re: Delete line 1 to n of a file

Posted: Mon Jan 30, 2023 12:46 am
by mwieder
I think Richmond's solution would be easier for newcomers to grasp, and if you replace "field" with "variable" it's functionally equivalent to what LiveCode does with Craig's version. While Craig's is syntactically concise and fast, it takes a fair bit of nogginwork to unravel.