Page 1 of 1

explode (split) content of a variable?

Posted: Sat Aug 16, 2003 10:48 am
by j0sh
in php i can split a variable by using

$variable = explode("ter_to_search_for",$variable)

then the variable becomes an array with all the split parts in it.

how can i do this in purebasic?

big thx and stay tuned!

j0sh :)

Re: explode (split) content of a variable?

Posted: Sat Aug 16, 2003 11:12 am
by PB
Maybe the StringField command? See here:

http://www.purebasic.com/documentation/ ... field.html

Is that what you mean?

Posted: Sat Aug 16, 2003 2:37 pm
by Kale

Code: Select all

array explode ( string separator, string string [, int limit])


Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string separator. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
This is what he means :D

Posted: Sat Aug 16, 2003 6:45 pm
by Karbon
Literally done in 5 minutes - so it's messy!

Remember to turn on inline ASM! If you don't want to use inline ASM check out this :

viewtopic.php?t=3796

.. and replace the CountChars function with it..

Code: Select all

;
; Remember to turn on inline ASM!
;

Procedure.l CountChars(a.s,s.s)
  !MOV edi,dword[esp]    ;pointer to the first character in string (first function parameter)
  ;firstly we must found the lenght of the string:
  !CLD              ;clear DF (Direction Flag). Could be not necessary.
  !XOR al,al       ;set NULL character in AL register
  !XOR ebx,ebx  ;init counter
  !MOV ecx,ebx    ;lets set 4294967295 ($FFFFFFFF) characters maximum
  !DEC ecx
  !REPNZ scasb    ;repeat comparing AL CPU register content with [edi]
  !JECXZ go    ;if NULL byte is not found within those 4294967295 characters then exit giving 0
  !NOT ecx     ;else, some adjusts. Now we have the lenght at ecx register
  !MOV edi,dword[esp]     ;point again to the first character in string (first function parameter)
  !MOV eax,dword[esp+4]
  !MOV al,byte[eax]    ;al=character to find
  !@@:REPNZ scasb   ;repeat comparing AL CPU register content with [edi]
  !JECXZ go     ;until ecx value is reached
  !INC ebx       ;or a match is found
  !JMP @r       ;continue comparing next character
  !go:MOV eax,ebx   ;output the matches counter
  ProcedureReturn
EndProcedure 

my_string.s = "this is the string I want to split"

num_occurances.l = CountChars(my_string," ")

Dim a_explode.s(num_occurances)

For counter = 0 To num_occurances
  
   ;
   ; StringField indexes start at 1, not zero.. That's the reason for the +1
   ;
    new_string.s = StringField(my_string, counter + 1, " ")
    
    a_explode(counter) = new_string
    
Next

For counter = 0 To num_occurances
 
  Debug a_explode(counter)
   
Next