explode (split) content of a variable?

Just starting out? Need help? Post your questions and find answers here.
j0sh
New User
New User
Posts: 3
Joined: Thu Aug 14, 2003 10:17 pm
Location: switzerland
Contact:

explode (split) content of a variable?

Post 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 :)
www.josh.ch - webWare for you
- phpMyWebmin, a browser based file management tool for your server
- Admin's Control Panel, a template-like webSite content managing system

gr33tz & have fun! :) j0sh :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: explode (split) content of a variable?

Post by PB »

Maybe the StringField command? See here:

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

Is that what you mean?
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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
--Kale

Image
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post 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
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply