deleting a line in a variable

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

deleting a line in a variable

Post by magice » Mon Jun 06, 2011 7:05 pm

I am working on a project that requires reading a txt file that is exported from another program and retrieving only the relevant lines of data. The irrelevant lines all begin with a "1", "0", or ",". I have made several attempts to eliminate these lines from the extracted variable but have had no luck. Also, the file I am reading from will always be 40 lines long. The following is what I have tried.

Code: Select all

  open file "attendance.txt" for read
   
   read from file "attendance.txt" until eof
   close file "attendance.txt"
   put it into myVar
   repeat with i = 40 to 1
      
      if the first character of line i of myVar is "1" or "0" or ","  
      then 
         delete line i of myVar 
         end if
end repeat
I have also tried:
breaking the if statement into 3 separate if statements, one for each of 3 possible first characters.
putting the if statements in a do command thinking that the variable "i" might not be being read properly
using "character 1 of line i" instead of "the first character of line i"
using "put empty into line i of myVar" instead of "delete line i of myVar"
placing myVar into a text box and trying to remove the lines from the text box
every combination of the above changes I could think of

Any suggestions would be much appreciated.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10318
Joined: Wed May 06, 2009 2:28 pm

Re: deleting a line in a variable

Post by dunbarx » Mon Jun 06, 2011 7:39 pm

Syntax. Gotta have "down to". You also have to tell the parser to deal with lines. Think about this:

repeat with i = 40 down to 1
if the first character of line i of myVar is in "1,0,comma"
then
delete line i of myVar
end if
end repeat

Someone who was just starting out might also have written:

if the first character of myVar is "1" or the first character of myVar is "0" or the first character of myVar is comma

Craig Newman

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: deleting a line in a variable

Post by magice » Mon Jun 06, 2011 8:37 pm

I thought it quite odd at first. I pasted your code into mine. It worked, however it also deleted every line beginning with an a,c, or m. I suspected that somehow it read the word comma as individual letters. I removed the quotation marks and it works fine. Thanks for putting me on the right track.

kray
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 56
Joined: Sat Apr 08, 2006 5:28 pm
Contact:

Re: deleting a line in a variable

Post by kray » Mon Jun 06, 2011 8:44 pm

There's an easier way, using "filter" (oh and btw, you don't need the open/read/close; you can just use "put url"):

Code: Select all

  put url ("file:attendance.txt") into myVar
  filter myVar without "1*"
  filter myVar without "0*"
  filter myVar without ",*"
That should do the trick...
Ken Ray
Sons of Thunder Software
Email: kray@sonsothunder.com
Web site: http://www.sonsothunder.com

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

Re: deleting a line in a variable

Post by bn » Mon Jun 06, 2011 8:49 pm

Hi Magice,

what Craig is saying is that if you start with the first line and count up you have the following problem:

suppose line 2 starts with one of the your index chars and you delete it. Now you have one line less. The line that used to be line 3 before deleting line 2 is now line 2. Your counter advances to 3 and tests line 3, which was formerly line 4. The current line 2/former line 3 is not tested for your index chars.

All this is avoided if you count from the last line up to the first line. Suppose line 39 has your index char and you delete it. The counter i will next test for line 38. It does not matter that you deleted line 39. It will not be tested again.

That is why Craig said:
Gotta have "down to"
When I started coding I had to get my head around that one too...

Although Craig's script works there is a little quirk in it:

Code: Select all

if the first character of line i of myVar is in "1,0,comma"
Comma in this context is not the Livecode constant. It is a litteral and not recognized by Livecode as ",". It works since there are two commas in the string. You could just as well write

Code: Select all

if the first character of line i of myVar is in "10,"
KInd regards

Bernd

Edit: too late, you found it out by yourself, and Ken had an alternative solution. Oh well, got to write faster :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10318
Joined: Wed May 06, 2009 2:28 pm

Re: deleting a line in a variable

Post by dunbarx » Tue Jun 07, 2011 2:05 pm

Bernd

Right on about that comma. There is something silly about trying to be the first to answer, at the expense of thinking and testing.

Also, he did try to start with the last line, (i = 40 to 1) but did not have the syntax correct. I assume he has the sense of why this must be so. I had written something similar to your excellent discussion, but deleted it when I reread the post. But there ought to be a mini-lesson published on this very topic.

Craig

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

Re: deleting a line in a variable

Post by SparkOut » Wed Jun 08, 2011 12:20 pm

Filtration as per Ken's suggestion is easiest, but just to add to the mix - with a filter for 40 lines of data you won't notice any difference, but you could also think about the merits of "repeat for each..." over "repeat with i=..." looping method and create a new variable out of the required lines from the old.

Code: Select all

put url ("file:" & theFilePath) into myVar
repeat for each line tLine in myVar      
      if tLine begins with "1" or tLine begins with "0" or tLine begins with "," then
            put tLine & cr after myNewFilteredVar
      end if
end repeat
--remove the last trailing cr
delete the last char of myNewFilteredVar
You can put myNewFilteredVar into myVar and clean up (delete variable myNewFilteredVar) if that is necessary.
Repeat for each tends to be hugely faster than repeat with indexvalue, because the engine has to count every line to find the one pointed to by indexvalue, but doesn't in a repeat for each situation. With a small data file like this it will not be noticeable but could be worthy of consideration if you ever have to work with a larger set of data.

Post Reply