delete line from file

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

delete line from file

Post by Nakia » Wed May 30, 2012 11:57 am

Hi Again,

I am trying to use the below handler to go through a file and delete certain lines.
It is failing with the delete statement.. what have i done wrong?
Note. if i replace it with "put empty into line x.." it runs but just deletes the contents from the lines of the file. The lines still exist in the
file but are blank..

Code: Select all

on deleteFlight
   -- used to delete data from flights.dat file
   subtract 8 from lLineBegin
   repeat with x = lLineBegin to lLineEnd
      delete line x of URL ("file:" & specialFolderPath("Documents") & "/flights.dat")
   end repeat
   answer "Data successfully removed"
   resetForm
end deleteFlight

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: delete line from file

Post by bn » Wed May 30, 2012 12:06 pm

Hi Nakia,

the problem with deleting lines in a repeat with x = 1 to 10 is that if you delete line 1 former line 2 becomes line 1 etc.

The solution is to go "backwards"

repeat with x = 10 down to 1

when you delete line 10 former line 9 will still be line 9 and it works.

something like this should work

Code: Select all

on deleteFlight
      -- used to delete data from flights.dat file
      subtract 8 from lLineBegin
     repeat with x = lineEnd  down to lLineBegin
            delete line x of URL ("file:" & specialFolderPath("Documents") & "/flights.dat")
      end repeat
      answer "Data successfully removed"
      resetForm
end deleteFlight
Kind regards
Bernd

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: delete line from file

Post by Klaus » Wed May 30, 2012 12:16 pm

Hi guys,

why so complicated?
...
delete line lLineBegin to lineEnd of URL ("file:" & specialFolderPath("Documents") & "/flights.dat")
...
Hey, its LiveCode, isn't it? 8)


Best

Klaus

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: delete line from file

Post by bn » Wed May 30, 2012 12:32 pm

Hi Klaus,
why so complicated?
....
Hey, its LiveCode, isn't it?
your solution if obviously the most elegant.

But it doesn't hurt to understand the logic of the "repeat with x = 1 to 10" loop. Took me a while to find out that if you delete something this way you change the data you are working on. With unexpected results...

Kind regards

Bernd

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: delete line from file

Post by Nakia » Wed May 30, 2012 12:33 pm

interestingly neither of these options work.

Simply errors out when i run the handler in live code..
brings up a Chunk error..

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: delete line from file

Post by bn » Wed May 30, 2012 12:38 pm

Hi Nakia,
could it be that your variables lLineBegin and/or lineEnd are not defined?
I notice that lLineBegin has a leading l, lineEnd does not.
What is in lLineBegin and lineEnd if you set a breakpoint and look at them in the debugger?
Kind regards
Bernd

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: delete line from file

Post by Nakia » Wed May 30, 2012 12:42 pm

They are populating just fine.

if I place a break at the delete line my variable populate as below.
lLineBegin 21
lLineEnd 40
x 40

Thats why i am confused...

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: delete line from file

Post by Nakia » Wed May 30, 2012 12:45 pm

I don't think its the variables as i can run other command in this repeat loop and they work fine.
for example put empty, put "defined text"

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: delete line from file

Post by bn » Wed May 30, 2012 12:49 pm

please doublecheck for typos, otherwise I don't have an idea of what is going on.
Bernd

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: delete line from file

Post by Klaus » Wed May 30, 2012 2:04 pm

HI Nakia,

Yes, in your scripts "lLineBegin" and "lineEnd" are not defined or are these LOCAL variables?

Try this, maybe pass LineBegin and LineEnd as parameters to this handler?:

Code: Select all

on deleteFlight tStartLine, tEndLine
   if tStartLine = empty then
     answer "Empty StartLine!"
     exit deleteFlight
   end if

   if tEndLine = empty then
     answer "Empty EndLine!"
     exit deleteFlight
   end if

   ## Alyways good to check!
   put specialFolderPath("Documents") & "/flights.dat" into tFlightFile
   if there is not a file tFlightFile then
      exit deleteflight
   end if

    ## Read complete file into variable:
    put url("file:" & tFlightFile) into tFlightData

   ## Check if there ARE that much kines in it!
   if the num of lines of tFlightData > tEndLine then
     answer "Too few lines to delete!"
     exit deleteflight
   end if

  ## Now delete the lines and save the content of variable back to file:
   delete line tStartLine to tEndLine of tFlightData
   put tFilghtData into url("file:" & tFlightFile)
   answer "Data successfully removed"
   resetForm
end deleteFlight
Best

Klaus

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: delete line from file

Post by Nakia » Wed May 30, 2012 10:20 pm

Hi,

These are both local variables.
If i place a break in the script before the delete line of the handler I can see the variables populate with the correct values.

I am going to put the file into a variable, delete the lines from the variable, then re populate the file and see if that works.

P.S. i did check it all for spelling mistakes..

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: delete line from file

Post by jacque » Thu May 31, 2012 5:49 pm

What is the exact error?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: delete line from file

Post by Nakia » Sat Jun 02, 2012 12:28 am

was a chuck error (cant find expression at character 7)

Good news though, I was able to modify it once I placed in into a variable.
so problems solved..

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: delete line from file

Post by jacque » Sat Jun 02, 2012 3:12 am

Glad you solved it, there's usually more than one way to do things in LiveCode. The error was a script error, but we'd need to see your code to know exactly what was wrong.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: delete line from file

Post by Nakia » Sat Jun 02, 2012 3:37 am

thanks for your help.

Interestingly enough I have another issue that I am really struggling with and am considering an upgrade to see if it helps.
Are you able to tell me if the upgrade to LC 5.5 is free?

Post Reply