[Gambas-user] Catch NULL character
Doriano Blengino
doriano.blengino at ...1909...
Sat Mar 28 20:55:37 CET 2009
CelticBhoy ha scritto:
> This is the code where the problem comes up :-
>
> PUBLIC SUB Seperate(sComponent AS String, iRec AS Integer)
>
> DIM sElement AS String[13]
> DIM iStart AS Integer
> DIM iFinish AS Integer
> DIM iLen AS Integer
> DIM iLoop AS Integer
> DIM iSlen AS Integer
>
> iLen = Len(sComponent)
> IF iLen > 1 THEN
> iStart = 1
> FOR iLoop = 1 TO 11
> iFinish = InStr(sComponent, ",", iStart)
> iSlen = iFinish - iStart
> sElement[iLoop] = Mid$(sComponent, iStart, iSlen)
> IF sElement[iLoop] = NULL THEN sElement[iLoop] = "0"
> iStart = iFinish + 1
> NEXT
>
...
> example of file input :-
>
> Delour,1,20,0,0,7.95,,0,1.3,,0,0
> Martell,1,20,0,15.59,13.17,13.94,0,1.9,16.99,0,0
> Bacardi,1,42.8,0,23.99,23.15,24,0,1.55,0,0,0
> Bacardi,1,20,0,10.61,11,10,,0,12.99,0,0
> OVD,1,20,0,0,,23,0,1.5,13.75,0,0
> Morgans,1,42.8,0,24.19,22.65,23,0,1.55,0,0,0
>
So you are searching for empty strings/fields, not NUL characters! :-)
The statement "if sElement[iLoop] = NULL" could be correct, but
semantically ugly (it depends on the language; in C and others, a NULL
and an empty string are different); I would prefer to test against an
empty string:
if sElement[iLoop] = "" then...
The split() function could save code and CPU time:
DIM sElement AS String[] <-- note, no dimension
sElement = split(sComponent, ",") <-- this line does the work of
"FOR iLoop"
After separating elements, you want some float number. Perhaps, thanks
to automatic conversions of gambas, you could avoid to use val(), and
write things like "PSize[iRec] = sElement[2]". The statement TRY could
help to facilitate the assignment; instead of testing for empty strings,
you could simply TRY to do the assignment: this protects also from
invalid numbers.
Hope this helps - regards,
--
Doriano Blengino
"Listen twice before you speak.
This is why we have two ears, but only one mouth."
More information about the User
mailing list