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.

Newcomers DBPro Corner / Save, Load, Copy Arrays

Author
Message
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 11th Dec 2010 23:50
Hey peoples,

I've finally found some DBPro time with work calming down. I've also installed DBpro on my girlfriends netbook which makes it much more accessible whilst lounging about.

I've spent the weekend figuring out some basic array function. Firstly, here's what I'm trying to do, I'll simplify my problem to just name data.

In my project directory, I have 2 text files, firstname.txt and lastname.txt. Eventually this could end up as 20 text files, each holding their own data to bring into my program.

*Note: Eventually I want to combine these text files into one file, such as a csv file, but it's not a priority now as I've gotta leearn to walk before I can run.


Now, I've managed to write a page of code which loads each text file into its own array, so that firstname.txt fills up firstname() successfully.



Now this works really well. But the problem is that I'd like to have it in a function. I'm not sure how to manage a function, since the function would have to address a different array depending on the text file being loaded.

That's when I realised I could have some sort of textdump() array, in which the function would copy the text file into the textdump() array, and then I could hopefully copy the textdump() array into firstname() array, then undim/redim the textdump() array, then do it all again for the remaining txt files.

Here's where I got so far;




Stuffs working, and I'm understanding, but I think it's important to consult the experience on the forums incase I'm doing everything the hard/wrong/n00b way.

Cheers in advance for any help.
BFM
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 12th Dec 2010 03:00 Edited at: 12th Dec 2010 03:45
In speaking with my mate, he advised to forget the whole function system for what I'm trying to do.

So with that, I've tried a different approach, but am wondering again if it's the way to go...



Now I'm trying to figure out how to miss-match the names, so that they're random as such.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 12th Dec 2010 04:44 Edited at: 12th Dec 2010 05:27
Ok, I've got the names from the text file going into an array. Each array then feeds into a wild population user type array, which has a maximum population set. Seems to be working, but if anyone can point out some tips and tricks I would be very thankful.

I suppose the next thing on my list is tackle CSV data, pending on what replies I get.

Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 12th Dec 2010 22:35
Hi. Like the look of what you're coding. Nothing seems to be amiss with the way you've been coding although I'm no expert :p

Could you zip up your project with your text files so we can run it and have a look?

If you're really looking for tips, I suppose you could use a constant for MaxPopulation rather than a variable although I assume you're using a variable as you intend to vary the number as you develop the code... Are you sure you need dword variables? They are huge after all. You might be better of running with integers as they're still pretty big in dbp.

Good luck with this!
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 13th Dec 2010 03:39 Edited at: 13th Dec 2010 04:10
Thanks for the feed back. I've attached an example text file that works with this program. So far I haven't found too many problems, and thanks to the below;



I can now export User Defined Type Arrays since I'm using Matrix1's utilities. Brilliant stuff! Saving and loading UDT's is an absolute god send. Since this system is really starting to open doors for the way my games can be programmed and managed, I'll be looking at importing data via other methods over the coming days, either in the XML format, or the CSV format. I'll need to read up on the limitations and the purpose of such file formats, to see which one's best to be employed and where.

Otherwise, I can see myself managing thousands of data.txt files sooner than later! Oh, and with respect to that, if you start using this system of text files, download and use NotePad++, it's brilliant for mass text file editing!

EDIT: Which reminds me, I have Dark Data, so I should really investigate where and how I can use that too.

EDIT 2: Further reading of the DarkData help files indicates that it should facilitate for exactly what I'm trying to do.

*cracks knuckles*

I'll see if I can duplicate the code found in my 3rd post, but orientate it using Dark Data.

Attachments

Login to view attachments
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 13th Dec 2010 12:07
Hmm, I remember now why I never really got into Darkdata. The help files appear to be out of date and malfunctioning, making it pretty intense for amateurs like myself to figure out.

Although I can get bits & pieces of the Darkdata Readme code working, some source that builds the Solar System compiles but crashes once running, meaning I'll have to spend more time figuring Darkdata out, rather than using it on my game.

I've emailed Steele and also responded to his Darkdata thread, so fingers crossed he's still about and able to patch either Darkdata, the readme files, or both!

Also as a side note, it appears that some Darkdata examples conflict with Matrix1's Utilities, so keep that in mind if you plan on looking into these two plug ins.

I'm not sure what to focus on now with my game. Maybe I'll let this thread build up a few responses, and focus on writing some music for the time being.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Dec 2010 15:04
Quote: "some Darkdata examples conflict with Matrix1's Utilities"

What commands conflict? I can't promise to do anything about it. but I'd like to know.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 16th Dec 2010 04:38 Edited at: 17th Dec 2010 00:17
Quote: "What commands conflict? I can't promise to do anything about it. but I'd like to know."


G'day IanM,

Sorry, I should clarify that there aren't any conflicting commands between Matrix1 Utilities & DarkData, but rather the #CONSTANTS used in the DarkData help file conflict with commands found with Mastrix1 Utilites.

I'm glad you posted though Ian, as now I can personally thank you for such an awesome plug in. Currently I'm programming the Population Array as a UDT, then I'm using your UDT save commands to produce a population.dat file. Very cool.

If anyone is in touch with Steele, let me know, as I'd be interested in re-writing the DarkData help files for experience.

EDIT: Shameless hung over cut and paste.

My current show stopper is in the PopulationWild array. My logic is tying itself in knots.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 22nd Dec 2010 03:25 Edited at: 22nd Dec 2010 05:27
OK, time for another update on my people maker. I finally got the unique names array working! All that's left to do is randomize a final list for use in a game.

I'd appreciate if anyone can recommend alternate work flows or coding practices with regards to my work.



I'll spend the rest of this hopefully quite afternoon working on the randomization, and also adding other attributes, such as age and sex.

EDIT: Bugger. I've just found out that I can't delete 2D array elements! Erk, this puts a slight spanner in the works.

Double EDIT: Erk, this is trickier than I thought. I'm guessing I'll randomly fill a selected amount of unique names with NULL, then create a new array, but filter out the "NULL" entries as they're read out.

Triple Edit: Head = assplode
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 22nd Dec 2010 19:16 Edited at: 22nd Dec 2010 19:35
Hey BFM,

I was bored this morning so I threw together some code that does what I think you are trying to do. You'll never get duplicate names unless you place duplicate entries in the data files. Note that my code keeps the arrays the exactly correct size, a requirement that you have specified to me elsewhere while discussing the PeopleMaker project outside of this thread.

Keeping arrays the exactly correct size is very cumbersome. In accounting for that requirement, all add or delete operations on the array are going to be much more complex because you'll have to use a dummy array and redimension the original array, copying data back and forth each time you adjust the contents. The cost of absolute memory efficiency is an increase in CPU load to manage the arrays, which is a questionable tradeoff - if you're dealing with small arrays then the minimal benefit of exactly accurate memory management makes it hard to justify code complexity - readable code is a thing you will come to appreciate with experience. If you are dealing with supremely large arrays, duplicating that much data will melt your CPU and cost you more memory than you're trying to save anyway.

Nevertheless, I've written code that meets your specifications. The ZIP file at the following location contains source code with working data files and executable (very small download):

http://www.ausleague.com/files/PeopleMaker.zip

For your reference.

EDIT: On deleting elements: Dimension a dummy array of the same size as the one from which you wish to delete, and run a FOR...NEXT loop to clone the contents. Now undim the original array and redimension it with a size one smaller than it used to be. Run a new FOR...NEXT loop that clones all the contents back, with an exception IF/THEN:

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 4th Jan 2011 06:23 Edited at: 4th Jan 2011 06:28
Cheers for your help Agent. I managed to break your code in less than a second upon reviewing it today. I simply changed the first name txt file to 5 entries, but left the last name txt file with 20 entries. BAM, error.

Now that I've had a nice holiday, I'll be taking another crack at my program. Will use your advice as a guide though.

EDIT: In reading this thread, it's clear that I needn't worry about deleting array elements, but rather, create a new array and randomly pick names to place into that new final array.
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 4th Jan 2011 19:42
You mustn't have read all the comments in the code

I direct your attention to line number 71:

Quote: "` Note that the final population count cannot exceed the number of elements in either the firstnames or the lastnames array."


Using the scramble system I've shown you doesn't work if you want more people than there are names in either the firstname or the lastname list. It's easy to make it so that you can, but then you'll risk duplicate names.
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 4th Jan 2011 19:44
By the way, BFM; whenever you discuss an error in code, quote the error message. You're probably receiving a subscript out of range error, but you should always specify the error message whenever you're discussing one with others, or we won't always understand what you're talking about
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 4th Jan 2011 20:32
I only skimmed over the code as I didn't get a whole lot of time to play with it yesterday. But did you try my last attempt, as it gets around this problem. You can have say, 7 first names, and 50 last names and it will just process it without drama. I spent AGES thinking about the logic, but finally got it working with this...

Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 5th Jan 2011 06:44 Edited at: 11th Jan 2011 03:21
At first glance I can see what appears to be an immediate logic error in your code: Don't you want to check if datalimiter and databump are LESS than the array count? Otherwise you'll only be adding a name if there are no names left to add (logic error!). Less than or equal to, seems like the operator you want.

EDIT: The above logic error is not an error at all - you've done it correctly. I misunderstood your logic.

Even so, aren't you going to get the same list of names every time you run that code? There's no random selection, and there's no scrambling of lists.

Furthermore, it looks like you're just making a redundant array that contains identical information to the other two. There's no string, anywhere, that contains a full name. You're just making a copy of the firstname array into the first half (0, x) of combinednamepool, and copying the lastname array into the second half (1, x) of combinednamepool.

You might as well be doing this:



I believe that code will do exactly the same thing yours does. I don't see the use of that code, since you can just reference firstname and lastname directly instead of the two halves of combinedname.

Oh and by the way, my code will handle 7 firstnames and 50 lastnames too, but only if you don't want any more than 7 fullnames. Your code has exactly the same limitation. There's no way around this without risking name duplication.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 6th Jan 2011 01:43
G'day mate,

Quote: "Don't you want to check if datalimiter and databump are LESS than the array count? Otherwise you'll only be adding a name if there are no names left to add (logic error!). Less than or equal to, seems like the operator you want."


This is great feedback, it's exactly the thing that I need to get in practice with, but always forget about. A lot of the time I get my code working, but sometimes there's room for error which I have to stay on top of.

Quote: "Even so, aren't you going to get the same list of names every time you run that code? There's no random selection, and there's no scrambling of lists."


The idea is for me to develop a system where I have either txt files or csv files full of entries, that I can then pull into my code for use with my games. That way, the design of my game isn't restricted to DarkBasic Pro, but rather, I can get other mates involved who are good with writing, who can then fill out txt files of say, sword models. The once they've thought up 20 different types of swords, I can take their txt file and load it into my engine with out having to program anything further.

Currently, the code I've written loads the first and last names in the two text files, then combines them in every possible unique way. From there, I have a complete list of unique names in which I can then pull random names from.

Quote: "Furthermore, it looks like you're just making a redundant array that contains identical information to the other two. There's no string, anywhere, that contains a full name. You're just making a copy of the firstname array into the first half (0, x) of combinednamepool, and copying the lastname array into the second half (1, x) of combinednamepool."


Incorrect. Combinednamepool is a listing of all unique names, and not just a copy of the name pools. At this stage I'm keeping first and last names separate, and not combining them into one string.

Take another look at it. Hopefully you'll understand what I've done.

Don't confuse duplicate names with duplicate full names. As an example;

Firstname1 Lastname1
Firstname2 Lastname1

is not a duplicate full name.

Firstname1 Lastname1
Firstname2 Lastname2

is not a duplicate full name.

Firstname1 Lastname1
Firstname1 Lastname1

is a duplicate full name.

My code will handle the input data (once it's finished) regardless of how many entries and how those entries are to be intertwined with other inputs. That's the plan.
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 11th Jan 2011 03:18 Edited at: 11th Jan 2011 03:26
Ok, I see what you're doing now, but it's incredibly convoluted.

Let me reverse engineer your code into logic to be sure I understand what your code is doing:

1. Populate the first set of indices in combinednamepool with the first firstname, as many times as there are unique lastnames.

2. Populate the next set of indices in combinednamepool with the second firstname, again as many times as there are unique lastnames.

3. Repeat until we're out of firstnames.

4. Swing back to the top of the combinednamepool array, and start sticking lastnames on them, rotating through the lastnames array until each instance of the first firstname has a unique lastname.

5. Repeat for the set of second firstnames, another list of unique lastnames, and so on until we have every possible combination of names.

There's a much cleaner, easier to read way of doing this, if I've got your logic correct (your variables are abstractly named, making it harder to follow).

How about this:



That will give you every possible firstname-lastname combination. You'll have duplicate first names and duplicate last names but no duplicate full names. It will also be a sorted list - if you want to get random names you'll want to scramble this array first.

EDIT: For clarity, my code handles names in a manner reverse to yours. Your list groups by first names: All Steves together, then all Karls together. My list groups by families: All Smiths together, then all Browns together. Seems more logical that way.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 11th Jan 2011 04:32 Edited at: 11th Jan 2011 04:55
Your code;



is awesome. I spent like, 2 hours hurting my brain trying to get this exact system visualised. I knew I had to do a loop in a loop, but unfortunately it was just beyond my reach, so I ended up with an uglier but working version of the same thing, family grouping aside.

I'll practice what you've posted here. Brilliant stuff.

EDIT: Yup, spent 5 minutes tweaking it and it now works like a charm. Now it's time to work on the randomization selection sequence.

In further thinking, I've also just realised a new problem. Assuming that eventually this population is going to have a sex... I gotta figure out the boy names from the girl names.
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 11th Jan 2011 07:03
Whenever you have arrays that are more than one dimensional, you'll find yourself using nested loops (loop within a loop) frequently to access or set the data in them. Once you've seen them a couple of times in 'given' scenarios like this, things will start clicking in your mind and you'll start to see all kinds of ways to use them.

The only way to delineate a male name from a female name is to specify that in the files with an additional parameter, or to use two separate files, one for male and one for female. Complicates things a little, but not too difficult.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 12th Jan 2011 12:20 Edited at: 13th Jan 2011 07:22
Ok, here's my latest cut. All systems go!



I'm going to spend more time on working out how to import CSV data OR txt file data. Sooner or later I'll be pumping some data that I've got stored in CSV format into my games, so it's best I start figuring that out sooner than later. Will hopefully have this CSV import code sorted by the end of this week.

EDIT: And thanks a few hours spent on the forums this morning, I do believe I've cracked it. Here's my first attempt, I was surprised it worked first go too!!



It'll need some refinement here or there. But it works, and now I can code up all my data for my games in Google Docs, then export the data to a CSV and it inserts straight into my game!

This is brilliant... It's such a dynamic way of coding!

EDIT: Now, I need to have a serious think on how to move forward with my projects now that I have an understanding these tools.

ANOTHER EDIT LOLCAT: In further thinking, I think it's best to keep data such as a list of first names, and a list of last names separate in two txt files, rather than bundled together in a CSV file. Problems encountered say, if I have 300 first names, and only 50 last names. Problems that can be avoided by not processing the data in CSV format, but sticking with two separate txt files.

Other data is perfect to be managed and then input via CSV, but not names and not for this system. So with that, I'll tidy up my code and post a final cut for this weekend.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 31st Jan 2011 03:56
Here is my final code for this project. I decided to practice some array pointer techniques too. All in all, the code works, reading 2 txt files full of first and last names, then combining them to produce a list of unqiue names, and then allowing the user to save some names to a CSV spreadsheet.

Kinda handy!



Help build an online DarkBASIC Professional help archive.

http://dbprohelp.wikia.com/wiki/DarkBASIC_Professional_Help_Wiki

Login to post a reply

Server time is: 2024-11-23 03:42:22
Your offset time is: 2024-11-23 03:42:22