Explode and Count Functions inside

BlitzMax Forums/BlitzMax Beginners Area/Explode and Count Functions inside

coffeedotbean(Posted 2010) [#1]
I had to use PHP for a bit and realised Blitz does not have an Explode function (unless I missed it). So I made my own, I also had to make a count function to to return the number of elements in an array. Works on Strict did not attempt to fix it for superstrict.

For those that dont know what explode is then... Explode returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

Added an Implode function.. why not. =D

Function syntax
Explode(String:String , Delimiter:string , [Add_Last_Part=True])

Implode(Array:string , Delimiter:String , [Add_Delimiter_To_End=False]

Count(array[]:String)


Example


Explode Function


Implode Function


Count Function



Czar Flavius(Posted 2010) [#2]
Do you know about array.Length?


degac(Posted 2010) [#3]
mmm

String.Join and String.Split are already present in BlitzMax and they work in a similar way of Explode and Implode


Jesse(Posted 2010) [#4]
we can all blame this thread on BlitzMax great documentation.


Czar Flavius(Posted 2010) [#5]
Well, it would have been good for the soul in any case.


coffeedotbean(Posted 2010) [#6]
HAHA no I didn't know about String.Join, String.Split or Array.Length.

But my main focus was retaining the simpleness of the explode and associated functions.

My_Array$[] = Explode(MyString$,MyDelimiter$)


Returns all the elements of the string in My_Array$[], You can then do this


for i = 0 to count(My_Array)-1
print i
next



handy for saving, loading level files that store data like:

1,0,2,3,1,67,32,12,34,8 etc..



Dreamora(Posted 2010) [#7]
its count(My_Array)-1 cause you start indexing at 0 not 1


coffeedotbean(Posted 2010) [#8]
^^ ah of course.... just testing you guys.


Oddball(Posted 2010) [#9]
I prefer using Until instead of -1.
For i=0 Until count(My_Array)
But that's just a personal preference.


Czar Flavius(Posted 2010) [#10]
Use an editor like Blide and the autosuggestions will allow you to discover all kinds of commands you never knew existed!


Kurator(Posted 2010) [#11]
these are your functions using methods provided by BMax

Function Explode:String[](Raw_String:String , Delimiter:String , Add_Last_Part:Int = True )
	Local result:String[] = Raw_String.Split(Delimiter)
	If Not Add_Last_Part Then
		result = result[..result.Length-1]
	EndIf
	Return result
EndFunction

Function Implode:String(Temp_Array:String[] , Delimiter:String , Add_Delimiter_To_End:Int = False)
	Local result:String = Delimiter.Join(Temp_Array)
	If Add_Delimiter_To_End = True Then result:+ Delimiter
	Return result
End Function

Function Count:Int(temp_array:String[])
	Return temp_array.Length
End Function



coffeedotbean(Posted 2010) [#12]
^^ nice one.

Think I need to try and find some info on methods, the only built in one I knew about was Sort.

Thanks all.


Htbaa(Posted 2010) [#13]
A quote from the Array documentation:
Arrays also provide a read-only length field that returns the total number of elements in the array


Although it's easy enough to overlook it :-).

But why use these functions? I find using array.Length, string.Join and string.Split easier and more natural.


coffeedotbean(Posted 2010) [#14]
Any way of finding the number of elements in a multidimensional array?

eg,

Width =19
Height = 14
Array[Width,Height]



Lets pretend we dont know that Width & Height is.

Array.length will return 266 which is Width*Height - all elements

Any way of finding the number of Width elements which in this case should return 19?

Array.Dimensions() will return the number of elements in the last Dimension, that's as close as I have got.

EDIT:

Ok I found a solution

[quote]
print Array.Dimensions()[1]
print Array.Dimensions()[0]
..etc
[quote]