|
-
Feb 22nd, 2001, 04:17 PM
#1
Thread Starter
Addicted Member
Hey, I'm almost there with the commentary thing, I can now load the text file and generate it randomly using arrays (i'm learning fast lol), I now have just one problem:
In the text file, all the commentary follows the line of:
P# passes the ball to R#
P# being passer, R# being receiver.
However, in the program, P# is already set as T1P1 (as string) and R# as T1R1 (ditto).
How do I make it so that when P# and R# are loaded, the program says that T1P1 = P# and the name of the player is put in place of it?
Thanks in advance...
(I'm almost done once I know how to do this )
-
Feb 22nd, 2001, 05:41 PM
#2
Fanatic Member
hmmm this looks familar. Check out your other thread.
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Feb 23rd, 2001, 02:31 PM
#3
Thread Starter
Addicted Member
i did already and that script wouldn't work, it wouldn't let me put P# as the thing to be replaced, I dunno what was wrong with it...
anyone have any alternatives?
-
Feb 23rd, 2001, 03:13 PM
#4
Fanatic Member
It worked perfectly for me scotty, if you post some code I can probably get it working for you.
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Feb 24th, 2001, 07:50 AM
#5
Thread Starter
Addicted Member
Right, I'm using the code you gave me. The variables are as follows:
P# = Passer in Text File
R# = Receiver in Text File
T1P1 = Passer in VB Program
T1R1 = Receiver in VB program
Do I have to declare P# and R# as variables in the text file? If so, I haven't done it... How do I do it?
Cheers...
CODE:
strSingles(0) = Replace(strSingles(0), "P#", T1P1)
strSingles(0) = Replace(strSingles(0), "R#", T1R1)
' I'm using this code in the Form_Load Sub
Public Function Replace(ByVal strSource As String, _
ByVal strWhat As String, _
ByVal strWith As String) As String
'***************************************************************************
'Purpose: Replaces all occurances of one string with another string.
'Parameters: strSource - String to be changed.
' strWhat - What needs replacing.
' strWith - What it should be replaced with.
'Returns: String - The changed string.
'***************************************************************************
Dim intstrWhatLength As Integer
Dim intReplaceLength As Integer
Dim intStart As Integer
intstrWhatLength = Len(strWhat)
If intstrWhatLength = 0 Then
Replace = strSource
Exit Function
End If
intReplaceLength = Len(strWith)
intStart = InStr(1, strSource, strWhat)
Do While intStart > 0
strSource = Left(strSource, intStart - 1) + strWith + Right(strSource, Len(strSource) - (intStart + intstrWhatLength - 1))
intStart = InStr(intStart + intReplaceLength, strSource, strWhat)
Loop
Replace = strSource
End Function
' This is the replace function
-
Feb 24th, 2001, 08:35 AM
#6
Frenzied Member
Well... hum... can you pleas elaborate a bit more on what you're trying to do? Is it a program that takes a text file with comments on a game and replaces the players' name or something? 
If you have an array with all the comments and the replace function, just do this.
You use players names in the text file in this form: T[team number - 1 or 2]P[player number - between 1 and 11]
Example: T2P7 passes to T2P3
Code:
Dim Comments()
Dim PlayersT1(1 to 11) As String 'The names of the players of team 1
Dim PlayersT2(1 to 11) As String 'The names of the players of team 2
PlayersT1(1) = "Name of player 1 of team 1"
PlayersT1(2) = "Name of player 2 of team 1"
PlayersT1(3) = "Name of player 3 of team 1"
'...
PlayersT2(1) = "Name of player 1 of team 2"
PlayersT2(2) = "Name of player 2 of team 2"
PlayersT2(3) = "Name of player 3 of team 2"
'...
'load each line of the file to Comments(), don't forget to ReDim it first with the number of players and that it starts at element 0
'...
For i = 0 to UBound(Comments) 'Loop trough all comments
For j = 1 to 11 'Loop trough all players
Comments(i) = Replace(Comments(i), "T1P" & j, PlayersT1(j)) 'Replace team 1 names
Comments(i) = Replace(Comments(i), "T2P" & j, PlayersT2(j)) 'Replace team 2 names
Next j
Next i
Hope that helps!
-
Feb 24th, 2001, 01:20 PM
#7
Fanatic Member
In order for the code that I sent you to work your text file should look like this.
"P# Passes to R#"
P# Throws a Hailmary to R#"
Etc.....
and with the Replace function used as shown above P# will be replace with whatever name is held in the T1P1 variable and R# will be replaced with whatever name is held in the R# variable.
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Feb 25th, 2001, 09:20 AM
#8
Thread Starter
Addicted Member
When I run the game, the debug highlights this line:
-> strSingles(0) = Replace(strSingles(0), "P#", T1P1)
strSingles(0) = Replace(strSingles(0), "R#", T1R1)
and says that the subscript is out of range?
What's going on? I did all of the stuff you told me...
-
Feb 25th, 2001, 12:42 PM
#9
Fanatic Member
That means that you are trying to access an element in the strSingles array that does not exist, how did you declare strSingles?
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Feb 25th, 2001, 02:09 PM
#10
Thread Starter
Addicted Member
Option Explicit
Dim strSingles(1 to 74) As String
However, when I change the code to:
strSingles(1 to 74) = Replace(strSingles(1 to 74), "P#", T1P1)
strSingles(1 to 74) = Replace(strSingles(1 to 74), "R#", T1R1)
It highlights the "=" sign and says "Expected End of Statement"
Any ideas now?
-
Feb 25th, 2001, 03:22 PM
#11
Monday Morning Lunatic
Replace acts on strings, not arrays. You'll need to loop through doing it to every element.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 25th, 2001, 04:00 PM
#12
Fanatic Member
To illustrate parksie's point...
[code]
Dim i as Integer
For i = 1 to 74
strSingles(i) = Replace(strSingles(i), "P#", T1P1)
strSingles(i) = Replace(strSingles(i), "R#", T1R1)
Next i
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Feb 26th, 2001, 05:35 PM
#13
Frenzied Member
Hey, that''s what I did!
My idea was to replace the names of the players of each team, but it can be easily chenged to "P" or "R". Like this:
Code:
Dim strSingles()
Dim Players(1 to 23) As String 'The names of the players
'Change it to the number of players: Team 1 + Team 2
'Example: Players 1-11 (11 players) are from team 1, players 12-23 (11 players) are from team 2
Players(1) = "Name of player 1"
Players(2) = "Name of player 2"
Players(3) = "Name of player 3"
'...
'Repeat for all the players - it's easier to keep them in an array than to have a lot of variables like "T1P1", "T1P2", "T1P3"...
Open App.Path & "Report.txt" For Input As #1 'Or any other file
i = 0
'Loop trough all the lines of the file...
Do While Not EOF(1)
'Redim the array to have 1 more element
i = i + 1
Redim Preserve strSingles(i)
'Fill the new element with the next line from the file
Line Input #1, strSingles(i)
'Loop trough all players
For j = 1 to 11
'Replace the names (so "P#6" would become the real name of the player 6)
strSingles(i) = Replace(strSingles(i), "P#" & j, Players(j))
'Same thing, for "R#"
strSingles(i) = Replace(strSingles(i), "R#" & j, Players(j))
Next j
Next i
Close #1
Don't forget his "Replace" function. You have to include it too! Try this, it should work.
If you want to keep the players in separate arrays or load the names of the players from a different file, just tell me.
-
Mar 3rd, 2001, 08:26 AM
#14
Thread Starter
Addicted Member
Thanks to everyone who helped, I finally got it working! Cheers people!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|