Populating an Array from Imported Data File

This is my first attempt at importing a data file into my program and I’m almost there.

The data file looks something like this:

0
34
56
82
119
169
230
302
381
464
546
624
694
756
808
853
891
925

etc…

To import the file and display in TextArea1:

[code]Dim f As FolderItem = GetOpenFolderItem(“text”)

If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open could raise an exception
Dim t As TextInputStream
Try
t = TextInputStream.Open(f)
TextArea1.Text = t.ReadAll //(Encodings.WindowsANSI)
t.Close
Catch e As IOException
MsgBox(“Error accessing file.”)
End Try
End If
End If[/code]

This works as expected with TextArea1 being populated (for my review purposes only).

Then I’ve tried to populate the array like this:

[code] redim tempArray(-1)
dim importedData as String
importedData = TextArea1.Text
tempArray = importedData.Trim.Split(EndOfLine.Windows)

//test
MsgBox str(tempArray.Ubound)[/code]

The MsgBox returns 0, therefore the array is empty.

Ultimately I would prefer not to have the TextArea, this is just temporary for me to check. It would be better if the array was populated directly from the file. I guess I could use ReadLine and loop until EOF, instead of ReadAll - but thought this would be more efficient.

Cheers.

If the msgbox shows 0 then the string is not empty, but rather has one element.

TmpArray() = split(importedData, endOfLine)

Thanks Roger. Yes, I guess there is one element.
“TmpArray() = split(importedData, endOfLine)” still doesn’t make any difference though.

However, I’ve discovered something: If I change the message box to MsgBox str(tempArray(0)) ie. zero instead of Ubound, I now get ALL of the data file showing in the message box. So, the whole of the data is being assigned to the first element of the array and not being split. At least that’s something.

I’ve been mucking around with this for the last few hours and getting nowhere fast.

When read back from a TextArea, the end of line markers will no longer be EndOfLine.Windows. It will be EndOfLine.Macintosh, on all platforms.

Well that’s done the trick - Thanks Tim. I’ll put that down to an idiosyncratic oddity of Xojo and NOT me this time :slight_smile: I won’t forget that one.

Now I’d like to bypass the TextArea altogether and import the file into a string. Some tips would be helpful.

tempArray() = Split(t.ReadAll,EndOfLine) ?

Yippeeee!!! that works perfect!

Thanks so much Roger, Tim and Emile.

This has been a break-through moment in my software and programming with Xojo. I’ve been working on the other parts and putting off File IO until now. I’ve learned a lot .

Cheers.

Both questions were answered.

This worked for reading the data from the TextArea:

This worked for reading the data directly from the file:

An often overlooked function is ReplaceLineEndings. That will let you normalize the EOL characters in a string before you process it, and is considered good practice especially when reading from a file whose originating platform is unknown.