home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2692
Discussions: 188,384, Posts: 2,243,502, Members: 232,614
Old February 18th, 2003, 03:08 PM   Digg it!   #1 (permalink)
Ultimate Member
 
cracked's Avatar
 
Join Date: Nov 2001
Location: Winston-Salem, NC
Posts: 1,440
Send a message via AIM to cracked
VB Coding Help #2 :D

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.
cracked is offline   Reply With Quote
Old February 18th, 2003, 03:22 PM     #2 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,135
Blog Entries: 7
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??

Whir is online now   Reply With Quote
Old February 18th, 2003, 03:31 PM     #3 (permalink)
Ultimate Member
 
cracked's Avatar
 
Join Date: Nov 2001
Location: Winston-Salem, NC
Posts: 1,440
Send a message via AIM to cracked
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

cracked is offline   Reply With Quote
Old February 18th, 2003, 05:17 PM     #4 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,135
Blog Entries: 7
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.
Whir is online now   Reply With Quote
Old February 18th, 2003, 05:33 PM     #5 (permalink)
Senior Member
 
cowboybooter's Avatar
 
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
cowboybooter is offline   Reply With Quote
Old February 18th, 2003, 05:38 PM     #6 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,135
Blog Entries: 7
Vass is the man. I am but a humble disciple.
Whir is online now   Reply With Quote
Old February 18th, 2003, 06:52 PM     #7 (permalink)
Ultimate Member
 
cracked's Avatar
 
Join Date: Nov 2001
Location: Winston-Salem, NC
Posts: 1,440
Send a message via AIM to cracked
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
cracked is offline   Reply With Quote
Old 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.
Creosote is offline   Reply With Quote
Old February 19th, 2003, 12:13 AM     #9 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,135
Blog Entries: 7
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.
Whir is online now   Reply With Quote
Old 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
vass0922 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2896)
CPU wont boot (5)
3-days in and no threads about Gaza (160)
The United States Debt (20)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
hp compaq nc6000 problems (138)
Folderchat Weekday thread (441)
Antec 300 bulk purchase? (11)
Recent Discussions
CPU wont boot (5)
Creative T-3000 Subwoofer (3)
ACPI controller halt on boot (2)
Building a gaming computer advi.. (3)
Worth the upgrade?? (15)
Folderchat Weekday thread (441)
ADVICE (0)
How to increase my ram? (5)
Help with an Ati Radeon HD 4850.. (27)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 10:08 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28