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 Discussion / Reading data denounced by comma's from a .csv formatted txt file

Author
Message
tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 9th Nov 2007 04:08
I made a nice spreadsheet in Excel with data for all my cards for Wartorn: Battlefields. I've exported it as .csv as that appeared the most easily parsable format for DBC. Then converted it to .txt. I've now reached a point where I cannot continue without some help.

The situation is as follows: in my original program I used hard returns to seperate data. I could replace all semicolumns with hard enters in the datafile, but I figured it would be more interesting and practical to keep the ";" denouncer.

The first line of the file is the number of cards in the array:



Then follow lines according to the following format:


I need to find a way to read the data between each ";" and put the data into the correct array. The last part I can do, but I do not know of a way to read data between the semicolumns. I've tried going through a string per character and finding out the position of the semicolumn and then using 'left$()' and 'right$()'. This kinda worked, was buggy and clearly inefficient.

Anyone?


A mod has been erased by your signature because it was larger than 600x120
pcRaider
18
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 9th Nov 2007 11:47 Edited at: 9th Nov 2007 11:53
Here is a link reading csv.
http://forum.thegamecreators.com/?m=forum_view&t=112491&b=1

Another method
You save it in CSV by Excel.
123,abc

You open in notepad.exe and replace it. [,]to[","]
123","abc

You convert it by a program.
DATA "123","abc"

In this, You can use you as data command.

tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 9th Nov 2007 12:58
Okay, but I need the file (data.txt) to be external. I might've forgotten to mention this, but I do want the external file to be read into an array.

Thanks for the help, tho' .


A mod has been erased by your signature because it was larger than 600x120
pcRaider
18
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 9th Nov 2007 13:57 Edited at: 9th Nov 2007 14:02
You can create files by using a program.

For example
tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 9th Nov 2007 15:49
I'm not really sure. I tried it, but the arr.txt file turns out to be unreadable in Notepad and thus uneditable. Is there any way to read the .csv file to put a single value denounced by semicolumns in a array, in such a way that the original file is editable by for example notepad?

So the textfile holds something like:


Which would be read as:


Like said, I could change all ; for hard enters. It wouldn't be much of a problem, but I'd rather avoid doing it - I'd like to know if there's a way to do the above in such a way that if I add a line to the file, it gets parsed by the game on the next run.


A mod has been erased by your signature because it was larger than 600x120
pcRaider
18
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 9th Nov 2007 17:17
I showed a link first.

DB does not have a CSV command.
You must make a function.
This is difficult.
Because, A program must correct a human mistake.

I show the link which you can learn.
http://forum.thegamecreators.com/?m=forum_view&t=113835&b=6
http://forum.thegamecreators.com/?m=forum_view&t=106556&b=6
tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 9th Nov 2007 19:25
Thanks for the help, PcRaider. I've checked the links but all of them are for DBP and unconvertable to DBC as far as I know.


A mod has been erased by your signature because it was larger than 600x120
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Nov 2007 20:46 Edited at: 9th Nov 2007 20:50
@tha_rami

It's not too hard to parse out the file the way you have it but you can save yourself a few headaches and a lot of coding if you save your file from excel a different way. Like pcRaider said, human error is where problems can occur - but as long as your file is consistent, you should be ok.

When you enter your data in excel you probably are doing it like:

Infantry|A single soldier|8|S| etc.

across a row with each piece of info in it's own column. And then the next line and so on and so on. That's fine. What we want to do is get this into a format that DBC can read easily and you want to get it into an array. Well, you could write a parser function, but you don't have to as long as your data is consistant. By consistant I mean there is always the same number of columns in each row. Your example has 18 items per row. This can be as many items as you want, you just want to make sure that each row has the same number (that's because we are going to convert it to an array).

Now, once you have your table of data in your excel spread sheet, you want to put that in a format DBC can read as an array. Easy enough. Open another spread sheet and starting from the left most column on your data table, copy each column into a single column on the new spread sheet. So if your data looked like:

mech man 5 3
boat sail 4 0

the new data would look like:

mech
boat
man
sail
5
4
3
0

Now save this new data as DOS text. Not csv or tab delimited.

In DBC you have to dimension an array that is the size of your data table. Remember, the indexes of an array are one less that the number of columns and rows of your table.

In your example, there are 3 rows, and 18 columns. So, set up a string array in DBC that reads:

Dim array$(2,17)

Now all you have to do is load your file into the array:

Load array "ramis_file.txt",array$(2) (only use the first dimension)

That's it! I know I wrote a lot, but the whole process takes no time at all. You can even write a macro that does all the copying for you into the new excel sheet into one column. The key is that the array is the proper size relative to your data table.

If you still want to parse out your file, dimension your string array to the size of your data. Create counting variables for each dimension (if your array was array$(3,9) - you'd have two counting variables index1 and index2)

Start reading your file one byte at a time. If any byte value comes through not equal to 59 ';' 13 (return) or 10 (line feed) add that byte to a temporay string variable


If the byte = 59, then set your array equal to the temp$, increment index2 and set temp$=""


If the byte = 10, it's the end of a line so set your array equal to temp$, increment index1 and set index2=0. Also set temp$="".

Repeat this process until the end of the file and you should have an array filled with the info.

Enjoy your day.
tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 10th Nov 2007 11:12
Thanks Latch - you saved me once again . Nice and clean solution there, jeez.

Thanks to you too, Pcraider, for your persistent help. The links were very useful as well. I might try them in DBP.


A mod has been erased by your signature because it was larger than 600x120
pcRaider
18
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 10th Nov 2007 12:34 Edited at: 10th Nov 2007 12:39
DBP is easy.
There is a CSV command in "Enhancements Expansion Pack".
There is .DLL, too.
.....................

A sample program of DBC



There is not an error check.
Be careful to unexpected accidents.

Login to post a reply

Server time is: 2025-05-31 20:30:48
Your offset time is: 2025-05-31 20:30:48