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.

DarkBASIC Professional Discussion / How to retrieve and also input a text into a string using delimiters?

Author
Message
noturno
15
Years of Service
User Offline
Joined: 9th Mar 2011
Location:
Posted: 10th Mar 2011 04:58
Dears,

What I am trying to do should be quite simple, but it appears to be not. I want to retrieve and also input pieces of text into a string trough delimiters (like a comma in the CSV).

To retrieve is very simple, be it with the free MatrixUtil1 or with Styx. In the case of Styx, for instance, it can be done with just a simple and powerfull command:

Return String=SPLIT$(Source String,Character,Index)

It means that we identify the string where to retrieve text, then identify the character used as delimiter and finally what of the delimiters should be used as a reference for retrieving. For instance:

SPLIT$("Peter/Mary/Peter/Ann/John", "/",2) = "Peter"

Ok, that is really easy and helpfull. But the problem is that there is not a command for doing the other way around: I would like to be able not only to retrieve, but to change the source string. For instance, how to select the delimiter position 2 to replace the second value (Peter)? Maybe it can be achieved trough functions, but I am having no success. Any help will be a very welcome here!
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 10th Mar 2011 10:15
Hi. Well as far as I know, there are no commands which do this although it could certainly be implemented as a plugin. Until someone does that, here are some functions I've just thrown together for you to use. I've tested them but as they're new, do tell me if they cause you any problems.



I'm not promising that this is the most efficient way to do this but it seems to work. InsertInDelimiter() allows you to add an item to the list at a given position without deleting any of the existing items. You can add items at the start of the list using the index 1 and at the end by giving an index which is one higher than the number of fields in the list. ReplaceInDelimiter() will swap an item at a given position for a new item of data. DeleteInDelimiter() will delete an item of data at the given position and replace it with nothing. CountFields() will tell you how many fields there are in your list.

This code should showcase what the functions can do.



Hope this helps. Just say if this is not what you're looking for. Regards.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Mar 2011 20:15
You can use SPLIT CSV STRING to split your CSV line into separate fields, using SPLIT COUNT to determine the number of fields and GET SPLIT WORD$ to retrieve each one.

To build your own CSV line, you can take each field in turn and pass it through DELIMIT CSV$, and combine them into a full CSV line.

You can use standard DBPro file commands to read/write the files, or you can use my own datafile commands (much faster).

These commands work correctly with CSV files generated by either Excel, or OpenOffice calc, even if your input/output fields contain commas or double quotes.

noturno
15
Years of Service
User Offline
Joined: 9th Mar 2011
Location:
Posted: 13th Mar 2011 21:42
Guys, sorry for tacking a bit long to come back after you gave me such fast and complete replies!

I thank you very much for them, as both ways seemed to be quite helpfull. Personally I like implementing the code, so liked very much Lucas' solution. But talking about Ian's, I got really interested in using the datafile commands.

One thing I had noticed is that i.e. for tile-based game to load each tile from a file, it takes very long in DBP if I use both Ini solution or an array previously loaded. Probably using other commands like Ian's datafile can help with that. I'll try them and create a new themed thread on that issue so to compare loading lots of tile data direcly from Ini, from an array or from Ini with Ian's commands.

Thanks, dudes!
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 13th Mar 2011 23:31
The DATAFILE commands are my favorite of IanMs commands. It's true power shows when you use them to load/edit small sections of a huge DATAFILE like for a database. Our programs wouldn't be as cool or efficient without his commands.

Login to post a reply

Server time is: 2026-07-17 13:59:42
Your offset time is: 2026-07-17 13:59:42