SPLITSTR Error...

Previous topic - Next topic

Stu_C

SPLITSTR ignores delimters that contain no data.  This is important for loading in CSV files  :S

take the code

Code (glbasic) Select

test$ ="1,,3"
num = SPLITSTR(test$, splits$[], ",")
FOR i=0 TO num-1
PRINT ">"+splits$[i]+"<", 0, 20*(i+1)
NEXT


I would expect the return to be
>1<
><
>3<

instead it ignores the empty array and produces

>1<
>3<

/Stu
www.ovine.net

Moru

To make debugging easier in these cases you can do a print in the console window like this:

Code (glbasic) Select
FOR i=0 TO num-1
DEBUG ">"+splits$[i]+"<\n"
NEXT


That way you don't have to keep track of pixels and stuff, just print to the console :-)

Quentin

#2
looks like a bug ... or a feature?

you can help yourself with using the REPLACE$ command in order to replace all occurrencies of ",," with ", ,". This should work.

Code (glbasic) Select

test$ ="1,,3,,,4,5,,,,,,6,"
test$ = REPLACE$(test$, ",,", ", ,")
PRINT test$, 0, 0
num = SPLITSTR(test$, splits$[], ",")
FOR i=0 TO num-1
PRINT ">"+splits$[i]+"<", 0, 20*(i+1)
NEXT
SHOWSCREEN
KEYWAIT


Kitty Hello

Feature.
The split skips any of the "split$" characters, until a non-split is found.

You want to split "a      b       c" into a,c,b. You don't want a,,,,,b,,,,,,c.
Replace is a good idea!

AndyH

That's one way of looking at it, but not one that you tend to see in implementations of split in other languages.  Same in Excel when you import a CSV with empty values, the commas define the column the data goes in.

EG:

NAME, TITLE, PHONE NUMBER
Andy,,01215559999
Stu,Ovine,
Gernot,GL Basic,0123232323

In GLBasic Andy, Stu and Gernot would be in the first array element but then in the second array element it would all go wrong... Andy's Title would incorrectly have the telephone number in, Stu would have Ovine and Gernot GL Basic.  Then the third array element would not exist for Andy or Stu but would have the telephone number for Gernot.

I guess the replace will work, but seems a bit unnecessary and perhaps not desirable if handling lots of splits.  Perhaps you should write a SuperSplit function instead Stu to handle it as required?

Stu_C

QuoteReplace is a good idea!

Suppose thats ok as a work around - but would prefer it to be automatic :)

/Stu
www.ovine.net

Kitty Hello

Write your own split then. Bacause in most cases you want multiple "splitters" to be dropped as one.

Stu_C


Ok,  I'll take it that I have been told  :good:

/Stu
www.ovine.net