BASIC

Author Topic: SPLITSTR Error...  (Read 2722 times)

Offline Stu_C

  • Mc. Print
  • *
  • Posts: 34
    • View Profile
    • OvineByDesign
SPLITSTR Error...
« 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<


Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1774
    • View Profile
    • Homepage
Re: SPLITSTR Error...
« Reply #1 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 :-)

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
Re: SPLITSTR Error...
« Reply #2 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
 
« Last Edit: 2008-Oct-06 by Quentin »

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: SPLITSTR Error...
« Reply #3 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!

Offline AndyH

  • Dr. Type
  • ****
  • Posts: 383
    • View Profile
    • http://www.ovine.net/
Re: SPLITSTR Error...
« Reply #4 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?

Offline Stu_C

  • Mc. Print
  • *
  • Posts: 34
    • View Profile
    • OvineByDesign
Re: SPLITSTR Error...
« Reply #5 on: 2008-Oct-08 »
Quote
Replace is a good idea!

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


Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: SPLITSTR Error...
« Reply #6 on: 2008-Oct-08 »
Write your own split then. Bacause in most cases you want multiple "splitters" to be dropped as one.

Offline Stu_C

  • Mc. Print
  • *
  • Posts: 34
    • View Profile
    • OvineByDesign
Re: SPLITSTR Error...
« Reply #7 on: 2008-Oct-08 »

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