Delete line 1 to n of a file

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bbalmerTotalFluency
Posts: 51
Joined: Mon Apr 06, 2020 1:19 pm
Location: Thailand
Contact:

Delete line 1 to n of a file

Post by bbalmerTotalFluency » Sat Jan 28, 2023 6:36 am

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.

Klaus
Posts: 13793
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Delete line 1 to n of a file

Post by Klaus » Sat Jan 28, 2023 11:14 am

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.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Delete line 1 to n of a file

Post by richmond62 » Sat Jan 28, 2023 1:24 pm

I would import the text into a field, delete the lines, and then write it back to the file.

Klaus
Posts: 13793
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Delete line 1 to n of a file

Post by Klaus » Sat Jan 28, 2023 1:40 pm

We guessed, Richmond, we guessed. :D

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Delete line 1 to n of a file

Post by SparkOut » 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.

Klaus
Posts: 13793
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Delete line 1 to n of a file

Post by Klaus » Sat Jan 28, 2023 1:58 pm

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

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Delete line 1 to n of a file

Post by SparkOut » Sat Jan 28, 2023 2:09 pm

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)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Delete line 1 to n of a file

Post by richmond62 » Sat Jan 28, 2023 10:26 pm

Klaus: if you can do something very, very easily (in the way I suggested) why spend donkey's ages on a more complicated way?

Klaus
Posts: 13793
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Delete line 1 to n of a file

Post by Klaus » 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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Delete line 1 to n of a file

Post by jacque » Sun Jan 29, 2023 6:38 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Delete line 1 to n of a file

Post by richmond62 » Sun Jan 29, 2023 7:25 pm

Yerss, well not all of are worried about shaving off a few nanoseconds. 8)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Delete line 1 to n of a file

Post by FourthWorld » Sun Jan 29, 2023 11:27 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Delete line 1 to n of a file

Post by mwieder » Mon Jan 30, 2023 12:46 am

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.

Post Reply

Return to “Talking LiveCode”