GLBasic forum

Main forum => GLBasic - en => Topic started by: Stu_C on 2008-Oct-06

Title: SPLITSTR Error...
Post by: Stu_C on 2008-Oct-06
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<

Title: Re: SPLITSTR Error...
Post by: Moru on 2008-Oct-06
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 :-)
Title: Re: SPLITSTR Error...
Post by: Quentin on 2008-Oct-06
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

Title: Re: SPLITSTR Error...
Post by: Kitty Hello on 2008-Oct-07
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!
Title: Re: SPLITSTR Error...
Post by: AndyH on 2008-Oct-07
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?
Title: Re: SPLITSTR Error...
Post by: Stu_C on 2008-Oct-08
QuoteReplace is a good idea!

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

Title: Re: SPLITSTR Error...
Post by: Kitty Hello on 2008-Oct-08
Write your own split then. Bacause in most cases you want multiple "splitters" to be dropped as one.
Title: Re: SPLITSTR Error...
Post by: Stu_C on 2008-Oct-08

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