Really weird stuff going on

BlitzMax Forums/BlitzMax Beginners Area/Really weird stuff going on

PowerPC603(Posted 2005) [#1]
I was trying to create a bunch of code to help kmac with his topic "Reading tabled data", but when testing, BMax does something weird and I cannot find the problem.

Global DataArray:Data[]

Type Data
	Field Name$
	Field Age%
	Field Height%

End Type

Function ReadTabledData(DFH%)
	Local LineFromFile$ = ReadLine$(DFH%) ' Read a line from the given filehandle

	Local a:Data = New Data ' Create a new "Data"-object

	a.Name$ = LineFromFile$[..10] ' Copy the first 10 characters from the line into field Name$
'	For i = 0 Until a.Name$.length
'		Print a.Name$[i]
'	Next
'	Print

	' Remove spaces at the end of the name
	While Chr(a.Name$[a.Name$.length - 1]) = Chr(32)
		a.Name$ = a.Name$[..(a.Name$.length - 2)]
	Wend

	' Resize the array to hold one more object
	DataArray = DataArray[..(DataArray.length + 1)]
	' Add the new Data-object to the array
	DataArray[DataArray.length - 1] = a
End Function



' Open a file for reading (DFH = DataFileHandle)
Local DFH% = ReadFile("c:\Test.txt")

' Keep reading until EndOfFile
While Not Eof(DFH%)
	ReadTabledData(DFH%)
Wend

' Close the file
CloseFile(DFH%)



' Print all names
For i = 0 Until DataArray.length
	Print Chr(34) + DataArray[i].Name$ + Chr(34)
Next

End


This is the file c:\Test.txt
Johnny    20   185
Piet      27   170
Geert     26   182
Gert      28   120
Cornelis  50   150


The code opens the file, creates a new Data-object if EOF hasn't been reached yet, reads an entire line, copies the first 10 characters into the Name$-field of this new object, removes spaces at the end of the name and prints all names.

The problem lies in the While..Wend loop following the line "' Remove spaces at the end of the name".
It does it's job perfectly for all names, except the "Geert" name (that's my name actually).

If you comment out the commented For..Next loop, it print all characters of each name (actually the ASCII code for each char).
You'll notice that the "Geert" name has these character codes: 71,101,101,114,116,32,32,32,32,32 (ending spaces included).

The code removes the last char (the "t"-char, ASCII code 116) too, but for the "Gert" name, it doesn't do it, there it works perfectly as only the ending spaces are removed and the "t" remains in the name.

I'm trying different approaches, as looping through the entire string backwards with a For..Next loop, storing each char in a different variable and checking that against Chr(32) (= ASCII code for a space), but all results where the same.

Can somebody explain this to me?


PowerPC603(Posted 2005) [#2]
Never mind, I've found the problem already.

	' Remove spaces at the end of the name
	While Chr(a.Name$[a.Name$.length - 1]) = Chr(32)
		a.Name$ = a.Name$[..(a.Name$.length - 1)]
	Wend


I was cutting off one character too much.
I thought the While..Wend loop did it's job good, as no other names had the problem.
It only worked with names that have a length that was even.

Just a math problem here actually (zero-based strings instead of one-based).


LarsG(Posted 2005) [#3]
perhaps it would be easier to read the fixed amount of characters into a string (the name) and then eventually strip that name of spaces?

just a thought..


PowerPC603(Posted 2005) [#4]
The code is already reading a fixed amount of characters (read the entire line and copy chars 0..9 to the Name-field).
After this copy, the ending-space removing routine (while..Wend loop) is run on that field.

Just replacing the spaces with nothing by using the Replace-command wouldn't be good, because you can have a first name and a last name, alltogether as the entire name.