Page 1 of 1
delete lines
Posted: Wed Oct 24, 2012 4:50 pm
by bsouthuk
Hi
Wonder if someone can help with something that should be very straight forward!
I am trying to delete lines/rows within a field but using the following script it only deletes some of the lines and not all:
Code: Select all
set the itemDelimiter to tab
repeat with i = 1 to num_lines
if item 7 of line i of tTariffDataGrid is "Share" then delete line i of tTariffDataGrid
end repeat
put tTariffDataGrid into field "Field"
Am I missing something from the code that is stopping certain lines being deleted?
Thanks
Daniel
Re: delete lines
Posted: Wed Oct 24, 2012 5:08 pm
by Simon
Hi Daniel,
Oh you have hit on a fun one I think.
Here is how I understand it, you have lines:
1
2
3
4
5
delete line 3
You now have
1
2
4
5
the position of line 4 is now line 3 and the "pointer" is now looking at the line with the number 5 in it.
Oh never mind!
This should do it for you:
Code: Select all
if item 7 of line i of tTariffDataGrid is "Share" then
delete line i of tTariffDataGrid
put i-1 into i
end if
Simon
Re: delete lines
Posted: Wed Oct 24, 2012 5:14 pm
by bsouthuk
Aaaah, got it - thank you very much!
Daniel
Re: delete lines
Posted: Wed Oct 24, 2012 5:36 pm
by Simon
OK I copped out on my answer, let me try again.
You have lines;
1[a]
2[a]
3
4
5[a]
repeat with i = 1 to 5
if "b' is in line i of myvar then delete line i of myvar
Ok so now i = 3
delete line 3
1[a]
2[a]
4
5[a]
on the next repeat i = 4, looking at the above and just counting 4 lines down gives us 5[a]
4 never gets inspected. So subtracting 1 from i returns it to 3 and 4 will get deleted.
Well thats a bit better.
Simon
Re: delete lines
Posted: Wed Oct 24, 2012 6:26 pm
by Klaus
Hi daniel,
you should use:
Code: Select all
...
put the num of lines of tTariffDataGrid into tNumOfLines
repeat with i = tNumOfLines down to 1
if item 7 of line i of tTariffDataGrid is "Share" then
delete line i of tTariffDataGrid
end if
## now happily delete all the lines that match your creteria :-)
end repeat
...
But better use the "repeat for each..." snytax, this is INSANELY fast!

More than thousands of lines in a fraction of a second!
Although "repeat for each" is READ ONLY (you cannot alter the lines) this technique is commonly and successfully used:
Code: Select all
...
repeat for each line tLine in tTariffDataGrid
## tLine is the ACTUAL content of that line!
if item 7 of tLine <> "Share" then
## you cannot modify tLine, so we simply create a new variable!
put tLine& CR after tNewList
end if
end repeat
## delete trailing CR
delete char -1 of tNewLine
put tNewLine into field "Field"
...
Best
Klaus
Re: delete lines
Posted: Wed Oct 24, 2012 6:43 pm
by bsouthuk
All understood - thank you very much for your help.
Daniel