I use the following code to item entered into a ExitText and append to an existing text file. I then sort it alphabetically. This all works perfectly but when I write the final text file (line 7 of the code below) it always finishes with a blank line. Can anyone help me delete this blank line. List.Size does not count the empty line either.
Here is the code:
Sub EditText1_EnterPressed
outFile.Initialize(File.OpenOutput(File.DirDefaultExternal, Text3, True))
outFile.Write (CRLF & Text2)
outFile.Close
List1.Initialize
List1 = File.ReadList(File.DirDefaultExternal, Text3)
List1.Sort(True) 'Sort list in descending alphabetical order
File.WriteList(File.DirDefaultExternal, Text3, List1)
EditText1.Text = ""
End Sub
1. Use code tags when posting code!
2. Why are you creating a new thread for your problem?
3. @Troberg already gave you the solution. So. After you read the list check for emtpy lines - and delete them if there are any - before you want to sort it.
Most probably it is better to post your problematic project (In the ide File-Export as ZIP) and upload this zip here describing what EXACTLY the problem is/what you want to archieve.
1. Use code tags when posting code!
2. Why are you creating a new thread for your problem?
3. @Troberg already gave you the solution. So. After you read the list check for emtpy lines - and delete them if there are any - before you want to sort it.
Most probably it is better to post your problematic project (In the ide File-Export as ZIP) and upload this zip here describing what EXACTLY the problem is/what you want to archieve.
Log("*************************************")
Dim test As List
test.Initialize2(Array As String("","Test","2","3","",""))
Log("*********** initial content *********")
Log(test)
test.Sort(True)
Log("*********** sorted ASC *********")
Log(test)
test = filteremptylines(test)
Log("*********** filtered *********")
Log(test)
Log("*************************************")
Sub filteremptylines(source As List) As List
Dim ret As List
ret.Initialize
If source.Size > 0 Then
For i = 0 To source.Size-1
If source.Get(i) <> "" Then
ret.Add(source.Get(i))
End If
Next
End If
Return ret
End Sub
Log("*************************************")
Dim test As List
test.Initialize2(Array As String("","Test","2","3","",""))
Log("*********** initial content *********")
Log(test)
test.Sort(True)
Log("*********** sorted ASC *********")
Log(test)
test = filteremptylines(test)
Log("*********** filtered *********")
Log(test)
Log("*************************************")
Sub filteremptylines(source As List) As List
Dim ret As List
ret.Initialize
If source.Size > 0 Then
For i = 0 To source.Size-1
If source.Get(i) <> "" Then
ret.Add(source.Get(i))
End If
Next
End If
Return ret
End Sub
Interestingly if I use List.Sort to sort a list items in a text file in alphabetical order it works correctly but appends a blank line to the end of the file.
Please see the attached zip file. The file to be sorted (Txt.txt) should be in the DirDefaultExternal directory.
List.Sort doesn't add items. You can put a breakpoint and see that there are 6 items after the call to List.Sort:
File.WriteList writes each item in a line and adds a new line character. This is the source for the extra line. Note that File.ReadList will still return 6 items.
List.Sort doesn't add items. You can put a breakpoint and see that there are 6 items after the call to List.Sort:
File.WriteList writes each item in a line and adds a new line character. This is the source for the extra line. Note that File.ReadList will still return 6 items.