PDA

Click to See Complete Forum and Search --> : Almost There!!!


scotty85_2000
Feb 22nd, 2001, 03:17 PM
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 ;))

YoungBuck
Feb 22nd, 2001, 04:41 PM
hmmm this looks familar. Check out your other thread (http://www.vbforums.com/showthread.php?s=&threadid=56400).

scotty85_2000
Feb 23rd, 2001, 01:31 PM
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?

YoungBuck
Feb 23rd, 2001, 02:13 PM
It worked perfectly for me scotty, if you post some code I can probably get it working for you.

scotty85_2000
Feb 24th, 2001, 06:50 AM
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

Jotaf98
Feb 24th, 2001, 07:35 AM
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? :confused:

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


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!

YoungBuck
Feb 24th, 2001, 12:20 PM
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.

scotty85_2000
Feb 25th, 2001, 08:20 AM
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...

YoungBuck
Feb 25th, 2001, 11:42 AM
That means that you are trying to access an element in the strSingles array that does not exist, how did you declare strSingles?

scotty85_2000
Feb 25th, 2001, 01:09 PM
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?

parksie
Feb 25th, 2001, 02:22 PM
Replace acts on strings, not arrays. You'll need to loop through doing it to every element.

YoungBuck
Feb 25th, 2001, 03:00 PM
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

Jotaf98
Feb 26th, 2001, 04:35 PM
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:



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.

scotty85_2000
Mar 3rd, 2001, 07:26 AM
Thanks to everyone who helped, I finally got it working! Cheers people!