Reputation: 13
I have this table of info in text file:
CurrentOdometerReading PreviousOdometerReading GallonsOfGas GasPrice MPG TotalCost
500 200 15 2.55 20 $38.25
600 350 18 2.55 13.89 $45.90
700 510 17 2.55 11.18 $43.35
800 443 9 2.55 39.67 $22.95
950 801 8 2.55 18.63 $20.40
10000 8043 99 2.55 19.77 $252.45
11000 5004 150 2.55 39.97 $382.50
The first line actually contains the column names.The next lines represent the data matching to the column name. I am having really hard time to print(loop) this data in below manner in UFT/QTP:
If anyone had similar experience, I would REALLY appreciate your opinions. So far I wrote this:
Set Fso=createobject ("scripting.filesystemobject")
set txtfile=fso.OpenTextFile filepath
var1=txtfile.ReadAll
var1=split(var1,vblf)
numarray= ubound(var1)
For i = 0 To numarray
content=split(var1(i)," ")
numlinearr=ubound(content)
print content(i)
'For Iterator = 1 To numlinearr
'bol=split(content(Iterator)," ")
'print content(i)&" = "& bol(Iterator)
'Next
next
Upvotes: 1
Views: 369
Reputation: 326
Seems you have a nice beginning. Though what you miss is an specific array for your data definition. And it's better not to keep the same variable name for different uses(as you do with Var1)
Something like :
fileFull = txtFile.ReadAll
fileLines = Split(fileFull, vblf)
fileDataNames = Split(fileLines(0), " ")
numArray = UBound(fileLines)
numData = UBound(fileDataNames)
For LineNumber = 1 To numArray
content = ""
localDatas = Split(fileLines, " ")
For dataNumber = 0 to numData
content = content & fileDataNames(dataNumber) & " = " & localDatas(dataNumber)
Next
Print content
Next
The base idea is to store the first line elsewhere, as you're going to use it several times. That's why the first loop starts from 1, and not from 0(the base for any tabled created by the split function) : the line zero is something else.
Upvotes: 1