delete lines

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

delete lines

Post by bsouthuk » Wed Oct 24, 2012 4:50 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: delete lines

Post by Simon » Wed Oct 24, 2012 5:08 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: delete lines

Post by bsouthuk » Wed Oct 24, 2012 5:14 pm

Aaaah, got it - thank you very much!

Daniel

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: delete lines

Post by Simon » Wed Oct 24, 2012 5:36 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: delete lines

Post by Klaus » Wed Oct 24, 2012 6:26 pm

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

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: delete lines

Post by bsouthuk » Wed Oct 24, 2012 6:43 pm

All understood - thank you very much for your help.

Daniel

Post Reply