how to move a line of a variable to previous line

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

how to move a line of a variable to previous line

Post by magice » Sun Jan 19, 2014 12:45 am

I have a very large text file that I need to modify daily to make it an importable data base file. I am trying to automate this process. The file consists of several data base fields and is given to me by another department. The data gets slightly rearranged in the process so that 1 line of data may get moved to several lines in the text file. What I have to do is look at each line, and if it is not the beginning of the line of data, hit backspace and space to add it to the previous line. The first field of each line of data is a PO number and is always written as one letter and six number in quotations E.G. "A111111". It is my thought to use this criteria to determine if that line should be left alone or moved to the end of the previous line. My problem (so far) is that I am not sure how to tell the program to move a line to the end of the previous line and put a space between them.

Code: Select all

 open file "Import/import.TXT" for read
   read from file "Import/import.TXT" until EOF   
    put it into field fText
   close file "Import/import.TXT"
   repeat with i = the number of lines in field fText down to 1
      if the first character of line i of field fText is quote and the ninth character of line i of field fText is quote
         then beep
         else 
            --I need this part to move line i to the end of line (i-1) separated by a space
         end if
     
   end repeat

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

Re: how to move a line of a variable to previous line

Post by magice » Sun Jan 19, 2014 3:09 am

OK I solved it with lots of trial and error

Code: Select all

on mouseUp
    put 0 into tNum
   open file "Import/import.TXT" for read
   read from file "Import/import.TXT" until EOF   
   put it into field fText
   close file "Import/import.TXT"
   repeat with i = the number of lines in field fText down to 1
      if line i of field fText is empty
      then 
      delete line i of field fText
      end if
   end repeat
   repeat with i = the number of lines in field fText down to 1
  
      if the first character of line i of field fText is quote and the ninth character of line i of field fText is quote
      then
         add 1 to tNum
      else
         put i-1 into tN
         put line i of field fText into tLine
         delete line i of field fText
         put space & tLine after line tN of field fText
         
      end if
      
      
      
      
   end repeat

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

Re: how to move a line of a variable to previous line

Post by Klaus » Sun Jan 19, 2014 1:50 pm

HI magice,

glad you made it work! :D

Please allow me to give to some important general hints:
1. Use QUOTES around object names! ALLWAYS!
fld "fField"
fld "tField"
etc...

2. When reading and writing to disk you might want to use the shorter URL syntax:
Instead of these 4 lines:
...
open file "Import/import.TXT" for read
read from file "Import/import.TXT" until EOF
put it into field "fText"
close file "Import/import.TXT"
...
You can have this ONE liner:
...
put URL("file:Import/import.TXT") into fld "fText"
...
FILE: for text files, BINFILE: for binary files

3. Tips for speed:
Do NOT access fields in a repeat loop!
This will slow down thing heavily!

Put everthing into a variable first, compute that variable and then write it back to a field.
Example:
...
repeat with i = the number of lines in field "fText" down to 1
if line i of field fText is empty then
delete line i of field fText
end if
end repeat
...
Better and MUCH faster:
...
put fld "fText" into tFText
repeat with i = the number of lines tFText down to 1
if line i of field fText is empty then
delete line i of field fText
end if
end repeat
## Now write content back to field:
put tFText into fld "fText"

But here you could even save some writing by using FILTER WITHOUT:
...
filter lines of tFText WITHOUT EMPTY
...
:D


Best

Klaus

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: how to move a line of a variable to previous line

Post by rkriesel » Mon Jan 20, 2014 9:39 am

Hi, magice. If you followed the good advice from Klaus, and all you still need is speed, try using "repeat for each line"

Code: Select all

command cleanText @rText
    local tText
    filter rText without empty
    put line 1 of rText into tText
    repeat for each line tLine in line 2 to -1 of rText
        if char 1 of tLine is quote and char 9 of tLine is not quote then
            put space & tLine after tText
        else
            put cr & tLine after tText
        end if
    end repeat
    put tText into rText
end cleanText
Here's a way to test that.

Code: Select all

on mouseUp
    local t1, t2
    put "a" & cr \
          & "b" & cr \
          & quote & "2345678" & quote & cr \
          & quote & "23456789" & cr \
          & "d" & cr into t1
    put t1 into t2
    cleanText t2
    breakpoint
end mouseUp
If you try it, do you see greater speed?

-- Dick

Post Reply