Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Easy string parsing

Author
Message
SirFire
20
Years of Service
User Offline
Joined: 4th Apr 2005
Location: North America
Posted: 8th Feb 2006 06:19 Edited at: 8th Feb 2006 06:21
Here is a function that makes parsing strings in DBP a peice of cake without the use of TPCs.

All you have to do is call the function like so:

The first argument of the function is the string you wish to parse, and the second argument is the delimeter you want to use to seperate the parts. In this case, we use the comma as the delimter, so the function will return an array of elements, and the bounds of the array will be the number of items found:
a(0)=milk
a(1)=eggs
a(2)=cheese
a(3)=bacon
You can use any character as a delimter including the space " ", which makes it handy for parsing plain lines of text.
so if you passed "Why did the chicken cross the road?" like so:

the function would return:
a(0)="Why"
a(1)="did"
....
a(6)="road?"

Hope this helps you in your programming endeavors!

The function:


Sven B
20
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 8th Feb 2006 21:04
No offence:



It's the programmer's life:
Have a problem, solve the problem, and have a new problem to solve.
SirFire
20
Years of Service
User Offline
Joined: 4th Apr 2005
Location: North America
Posted: 8th Feb 2006 22:25 Edited at: 8th Feb 2006 22:27
Quote: "No offence:"

None taken, I will now be using your code instead as it looks more efficient. Thanks

[edit]Although, since you declared the array inside the function and did not specify a return value from the function, some modification will be necessary to make the data generated available outside the function.

Sven B
20
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 9th Feb 2006 19:22
Quote: "Although, since you declared the array inside the function "


the array is declared in the function just like yours

Maybe something I should've said earlier:
I think a piece of your code is missing. You're referring to InStr() which isn't a command. So I guess you might've forgot a function

Oh and one thing, if you have edited my code, please post it here

It's the programmer's life:
Have a problem, solve the problem, and have a new problem to solve.
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 13th Feb 2006 08:27
I made this about 2 years ago (its in the codebase), a string tokenizer. It can break down a string given multiple single-character delimiters at once.




BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Feb 2006 09:49
SirFire, you can get the same info from Sven B's code. He's adding elements to the array when required, so ARRAY COUNT(a()) gets you the number of strings. It also means you only ever have as many elements as you need, and can have more than 200 results.

Login to post a reply

Server time is: 2025-05-15 22:07:34
Your offset time is: 2025-05-15 22:07:34