February 18th, 2003, 03:08 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: Nov 2001 Location: Winston-Salem, NC
Posts: 1,440
|
hey yall. i need some more help w/ vb. i have a program written. it is a random sentence generator. the way it works is it has a builtin list of 30 verbs, 30 nouns, adverbs, adjectives, etc. it randomly picks one from each list and puts the words together to make a sentence. what i want to change about it is i want to have the program select words from an external list which can be easily edited instead of having the word lists built into the program. like i could have a .txt or .dat file or something with words in it that the program will select from. how can i do this? i used to know how to do it in Qbasic but i have forgotten and im sure its different anyway. thanks!
drew
EDIT: is there a good VB help website out there so i dont have to keep posting this stuff? thanks 
__________________
Visit http://duroo.org
Last edited by cracked : February 18th, 2003 at 03:12 PM.
|
| |
February 18th, 2003, 03:22 PM
|
#2 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,135
|
If you want to use a text file, you can either use a simple Open statement and then Read, or you can get funky and use the File System Object (which though is a bit more work is much easier IMO).
I don't remember the exact syntax for either, but you can find quite a bit of info on how to use File System Object on Google. I can't remember any good help sites offhand at the moment, but I usually just googled for what I was trying to do and found something.
What I would do is create a single file, and then create unique headers inside the file, so say you had this:
[NOUN]
noun
noun
noun
noun
noun
[VERB]
verb
verb
verb
verb
verb
etc
Then read the entire file into different arrays and randomly pull values out of those arrays (I am an array fiend  ).
Does that make any sense?? |
| |
February 18th, 2003, 03:31 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: Nov 2001 Location: Winston-Salem, NC
Posts: 1,440
|
yes, it makes sense, but ive never done arrays enough to get the hang of them. could you explain arrays or point me to a site that does? or would it just be easier to do the open&read thing for me?
drew |
| |
February 18th, 2003, 05:17 PM
|
#4 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,135
|
Well, either way I would use an array. Here's something like what I would do...
Do declare something as array, just put a () behind the variable name. Code: Dim sNouns() as String
Dim sVerbs() as String
Dim sAdjectives() as String Now what that does is makes an a non dimensioned array for use. You could also make the size static by putting a number inside the ()'s. For instance, Code: Dim sNouns(10) as String This would create an array with ten spaces for string variables. Sort of like saying you've got a CD rack that holds ten CDs, except those CDs are your strings.
Since you want to be able to add words to your lists, we don't want a static array, we want to be able to redefine it each time. We are going to want to employ While loops to read the text file into these arrays.
After you've created your File System Object, or Open object, you'll want to throw it at the first loop, which will look for one type of word. For this, we will assume that 'fso' is your File System Object (the text file): Code: While Not fso.EOL
If fso.ReadLine = "[NOUN]" Then
i = 0
While Left(1, fso.ReadLine) <> "["
ReDim Preserve sNouns(UBound(sNouns) + 1)
sNouns(i) = fso.ReadLine
i = i + 1
Wend
If fso.ReadLine = [VERBS] Then
etc
End If
Wend That make any sense? Probably not, I think I confused myself... :P
This line: ReDim Preserve sNouns(UBound(sNouns) + 1) sets the array to one size larger than it was previously while preserving the data inside. If you leave out the Preserve keyword, it will erase all the contained data when it is resized. You could also use the i instead of UBound(), but UBound will always be correct where as i may not for one reason or another.
Edit: I can't code. This won't work in real life, you'd need to make a string variable to hold fso.ReadLine for use within the loops it'd be skipping all over the place and wouldn't work right.
Last edited by Whir : February 18th, 2003 at 05:24 PM.
|
| |
February 18th, 2003, 05:33 PM
|
#5 (permalink)
| | Senior Member
Join Date: Jul 2002 Location: London Suburbia,UK
Posts: 531
|
I agree with Whir's first statement, it's easy to create a txt file, open and read it, the syntax for this I have in my coursework, but I can't get into the darn cupboard right now !( One of very few VB questions I could make sense of, and I cannot get at my notes,  )
These days, if I get stuck I ask Vass0922 ( bow's reverendly ) a VB Guru.
CBB |
| |
February 18th, 2003, 05:38 PM
|
#6 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,135
|
Vass is the man. I am but a humble disciple.  |
| |
February 18th, 2003, 06:52 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Nov 2001 Location: Winston-Salem, NC
Posts: 1,440
|
thanks Whir! ill try this out and see if i can make sense of it. ive looked all over the web and i cant find anything to help me out really. but what you put i think will work great. thanks a lot!!
drew
Vass, if u have comments please add them  |
| |
February 18th, 2003, 10:04 PM
|
#8 (permalink)
| | Member
Join Date: Sep 2002
Posts: 364
|
I would put them in an Access file. Or you can probably even use ADO to read a text file, if its set up right. Read all the records into an array, and select a random array element. Or if you used MSDE to store it in, you could pull random records without using a random number in your program. |
| |
February 19th, 2003, 12:13 AM
|
#9 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,135
|
I recommend a flat text file in this case. You will not have to rely on the machine having Access components installed, or Access itself to edit the list of words. |
| |
February 22nd, 2003, 07:48 PM
|
#10 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 21,019
|
I think Whir is going to replace me as guru 
Sorry I didn't comment earlier, I don't visit down here as often these days
I agree with whir that you can easily place them in a text file. Using Open and Read is an easy way to work with things, and it has its good points, I still like to use the FSO more .. that and I use a alot of scripting and thats all that is available to VBS.
You could use a [NOUN] header as whir says, or another option would be to use a different text file for each word type *shrug* thats the fun part about programming, there's a million ways to do one thing
Whir you are correct, you will have to assign .ReadLine to a variable, or EVERYtime you do a readline it will move down one line 
So at the top do a readline into a variable and use the variable everytime within the same iteration of the loop.
Also one bug you have is EOL doesn't work with FSO (TMK)
While Not FSO.AtEndOfStream  |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |