Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text string to variables

Status
Not open for further replies.

bigbiceps

IS-IT--Management
Sep 29, 2005
6
GB
I have not had much practice with manipulating text files... i can read in a line with line input or a string with mid but what i want is to assign parts of the text file to variables.
File might look like this
Name bob
date 29/09/2005
time 10:00:00

i would like to assign bob to a vaiable such as myname
and 29/09/2005 to a variable mydate etc etc

i have searched but cant see the wood for the trees




 
Well, there is a trade off here between making the program flexible and forgiving vs. a speedy solution. Will the format of the text file be set in stone?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Dim myName As String
Dim myDate As Date
Dim myTime As Date ' not sure, do not remember

1.
You can read line by line and use pos=Instr(1st line," ") to get the position of the space. Then get "Bob" by: myName=CStr(Right(1st line,Len(1st line)-pos+1))

etc for the others...
NOTICE:
CStr : converts to string
CDate : converts to date

2.
Split the line using " " (space) as delimiter and this you want the 2nd item of the array or the item at index 1. NOTICE: that arrays are zero-based.
 
As Tom said, you'll have a little more trouble if the order or even the size of the files is diffent.

Think of storing to Date CDate("Bob")
-or-
have Email same texts files (so 4 elements) and ommit the last...
 
I would also consider using a dictionary. Then you could read each line and do something like:

oDictionary.Add(LCase(Split(strLine, " ")(0)), Split(strLine, " ")(1))

Then later to get the name you could do someting like:
MsgBox oDictionary("name")

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
thanks for the info
the file will be the same at the top but will have 1 or 2 or 3 entries for the time eg
name bob 'this would be assigned as myname
date 29/09/2005 'this would be assigned as mydate
segments = 2
details segment 1
start time 10:00:00
stop time 10:15:00
details segment 2
start time 10:30:00
stop time 10:45:00

if the required segment is segment 2 then i want to assign mytime1 to segment 2 start time and mytime2 to segment 2 stop time
 
Read all the file and check the value of segment.If is 1 then deal with next 3 lines else the last 3. Anyway, nothing is standard. You build the way that data get read/handled.

Later on writing us down some code (showing us the way you thought of) it will be easier fix it, add ...
 
Hello
I have something like this and it works but i am sure that this can be coded better..i have put a sapce between name and date and segment lines


Dim intFileNumber As Integer
Dim strLine As String
Dim myName As String
Dim myDate As String
Dim mySegment
Dim myStartTime
Dim myStopTime

mySegment = InputBox("What is the segment number?")

intFileNumber = FreeFile
Open "E:\DOCUMENTS\test.txt" For Input As #intFileNumber
Line Input #intFileNumber, strLine
myName = Right(strLine, 5)
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine

myDate = Right(strLine, 10)
If mySegment = 1 Then
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
myStartTime = Right(strLine, 8)
Line Input #intFileNumber, strLine
myStopTime = Right(strLine, 8)
Else
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
Line Input #intFileNumber, strLine
myStartTime = Right(strLine, 8)
Line Input #intFileNumber, strLine
myStopTime = Right(strLine, 8)
End If


MsgBox ("Name" & myName)
MsgBox ("Date " & myDate)
MsgBox ("StartTime " & myStartTime)
MsgBox ("StartTime " & myStopTime)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top