PDA

View Full Version : Remove blank lines


cchian
03-26-2004, 10:48 AM
Listed below is part of a sample code someone posted for AMS4, the code deletes blank lines from a text file. I am now trying to see if I can do the same in AMS5 Pro but I am having difficulty finding equivalent actions . Here is the AMS4 code I am trying to duplicate:

%LineCount% = TextFile.CountLines ("%TempDir%\myfile.txt")
%Ctr% = "0"
WHILE (%Ctr% < %LineCount% )
%TextLine% = TextFile.GetLine ("%TempDir%\myfile.txt", %Ctr%)
IF (%TextLine% = "")
TextFile.DeleteLine ("%TempDir%\myfile.txt", %Ctr%)
%LineCount% = Evaluate (%LineCount% - 1)
ELSE
%Ctr% = Evaluate (%Ctr% + 1)
END IF
END WHILE

Do I need to use both Tables and Paragraphs?

TJ_Tigger
03-26-2004, 03:06 PM
Something like this might work


tblTextFile = TextFile.ReadToTable("filename.ext")
for index, row in tblTextFile do
if row == "" then
tblTextFile = Table.Remove(tblTextFile, index)
end
end

cchian
03-27-2004, 01:11 AM
TJ_Tigger, thanks for the help. I am still having some difficulty trying to add your code into a really cool sample project from Autorun.org called "Text File and Tables". Download (http://www.autorun.org/modules.php?name=Downloads&d_op=getit&lid=41)

I want to add a button that will remove all blank lines and display the results in the paragraph window. I will do some more reading and experimenting and hopefully I will be able to figure it out soon!

cchian
03-29-2004, 12:43 PM
I am trying to delete blank lines from a text file. I am getting this error "On Click Line 10: Argument 1 must be of type table." This is Line 10: "tFileContents = Table.Remove(tFileContents, index)."

Thanks,

Here is what I have so far:

tFiles = Dialog.FileBrowse(true, "Browse", "", "Text Files (*.txt)|*.txt|", "", "", false, true);
if tFiles and tFiles[1] ~= "CANCEL" then

for index, row in tFileContents do
if row == "" then
tFileContents = Table.Remove(tFileContents, index)
end
end

Worm
03-29-2004, 01:00 PM
You missed reading the file into a table like Tig suggested.


tFiles = Dialog.FileBrowse(true, "Browse", "", "Text Files (*.txt)|*.txt|", "", "", false, true);

--you only need to check tFiles[1] for CANCEL
if tFiles[1] ~= "CANCEL" then

--*************************************
tFileContents = TextFile.ReadToTable(tFiles[1])
--*************************************

for index, row in tFileContents do
if (String.TrimLeft(row, " ") == "") or (String.TrimLeft(row, " ") == "\r\n") then
tFileContents = Table.Remove(tFileContents, index)
end
end
end

cchian
03-29-2004, 02:58 PM
Worm, could I be something else? I tried your code in a new project and I get the error about "Argument 1 must be of type table" with some text files but not others.
Seems like if there is a blank line in the text file, then I will get the error although that did not happen with another file. Strange...

Worm
03-29-2004, 03:09 PM
I really need to start testing this stuff before I post it :)

Try this:


tFiles = Dialog.FileBrowse(true, "Browse", "", "Text Files (*.txt)|*.txt|", "", "", false, true);

--you only need to check tFiles[1] for CANCEL
if tFiles[1] ~= "CANCEL" then

--*************************************
tFileContents = TextFile.ReadToTable(tFiles[1])
--*************************************

for index, row in tFileContents do
if (String.TrimLeft(row, " ") == "") or (String.TrimLeft(row, " ") == "\r\n") then
result = Table.Remove(tFileContents, index)
end
end
end

cchian
03-29-2004, 04:34 PM
I am trying to see the results by adding the following line after your code:

Paragraph.SetText("Paragraph1", result);

I did create a paragraph object, but looks like the code is deleting everything, not just blank lines. Am I missing something?

cchian
03-29-2004, 04:44 PM
I am trying to see the results by adding the following line after your code:

Paragraph.SetText("Paragraph1", result);

I did create a paragraph object, but looks like the code is deleting everything, not just blank lines. Am I missing something?

Worm
03-29-2004, 05:38 PM
The results are in the table tFileContents not in the variable result

do this:

tFiles = Dialog.FileBrowse(true, "Browse", "", "Text Files (*.txt)|*.txt|", "", "", false, true);

--you only need to check tFiles1] for CANCEL
if tFiles[1] ~= "CANCEL" then

--*************************************
tFileContents = TextFile.ReadToTable(tFiles[1])
--*************************************

for index, row in tFileContents do
if (String.TrimLeft(row, " ") == "") or (String.TrimLeft(row, " ") == "\r\n") then
result = Table.Remove(tFileContents, index)
end
end

--Build a variable that contains the tables content
strFile=""
for index, row in tFileContents do
strFile = strFile .. row .. "\r\n"
end
Paragraph.SetText("Paragraph1", strFile)
end

cchian
03-29-2004, 10:36 PM
Worm and TJ_Tigger, thanks for all the help!!! It is working great.