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..
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
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
why so complicated?
...
delete line lLineBegin to lineEnd of URL ("file:" & specialFolderPath("Documents") & "/flights.dat")
...
Hey, its LiveCode, isn't it?
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...
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
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
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.
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
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?