PDA

View Full Version : Split and Parse



marcuslee
06-11-2010, 21:21
I've been reading about these two keywords in the help file. There are differences in the descriptions, but it seems that the basic functionality is the same: Put a long string with delimiters into an array.

What are the differences (besides that Parse has more functionality with the matrix bit)?

What circumstances would require one over the other?




Mark

marcuslee
06-11-2010, 22:57
I've been playing around with INI files. I believe the following code demonstrates that on a simple level, Split and Parse can be used in the same way.



Uses "INI"

Dim x, n As String
Dim z(10), d(10) As String
Dim y,v,b As Byte
Dim iFile As String = APP_ScriptPath + "file.ini"

INI_SetKey(iFile, "1", "1", "23|26|65|76|87|43|21|35|43|99")
x = INI_GetKey(iFile, "1", "1")

y = Parse(x,z,"|")
v = Split(x,"|",d)

n = d(3) + z(2)

b = MsgBox (0,n)


Which reminds me of another observation I would like to make that doesn't have anything to do with this thread ... so I will post elsewhere.


Mark

ReneMiner
10-05-2013, 02:51
It would be interesting to know if there is some possibility to use the power of Parse-functions to get a certain element/line of some text-string.

Currently I know only how to Parse one string into a lot of small pieces seperated by some delimiter as $CRLF for example. But it always (?) creates/redims some array which I have to supply & dim in advance although sometimes I only need a certain element of that array.


Example:

I have some String sFileList consists of Filenames, maybe 2500 all in one string, delimited by $CRLF.

Now to access filename(333) I have to create some dynamic string array which gets redim'med by the parse-function once, thereafter I have to get element (filename) 333 by indexing and assigning it and drop the rest of parsed string-array thereafter to garbage again since I'm only interested in filename #333 and used up lots of unnecessary time for redim & reordering the memory .

Now I think about something like:


String myFilename = ParseElement$(sFileList, 333, $CRLF)

which will just pick out and assign string-element 333 (or "" if element 333 not available) to myFilename without the need to create/redim additional variables.

Is there already some smart way to do so?

ErosOlmi
10-05-2013, 07:16
If I understand well what you need, PARSE$ does what you need: http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?parse$.htm



String myFilename = Parse$(sFileList, $CRLF, 333)


The only problem with PARSE$ is that it always parse the string so it is quite inefficient to repeatedly use PARSE$ especially when the string is quite big.
In those cases, it is better to use PARSE (without $ sign) and create an array. But if you need to use it once in a while, no problem.

Ciao
Eros

ReneMiner
10-05-2013, 10:56
OK, thanks. Now I can see the difference between Parse and Parse$ :)