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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to modify numerical value in a text file 2

Status
Not open for further replies.

Pegase31

Technical User
Aug 31, 2009
3
LU
I would like to multiply all values in some big text file and replace them with the new one.
My text file is similar with the example below and it is only OFFSET value.

Your help will be appreciated.

OFFSET 0.00000 -0.00000 1.02054
CHANNELS 6 Xposition Yposition Zposition
JOINT LHipJoint
OFFSET 1.37959 -1.78713 0.86582
CHANNELS 3 Xposition Yposition Zposition
JOINT LeftKnee
 
I tried a lot of things but my principal problem is to find numerical value, the sign and the dot.
If I can isolate it, the replacement will be not a problem for me.
 
I'm sure there is a better way to do it but you can compare individual strings against a string of numerical digits and signs. Compare each string line-by-line to avoid resource consumption. Also, just like in court, all strings are considered valid until proven otherwise.

strValidChars = "0123456789+-.,"

set objStream = objFSO.OpenTextFile("file.ext", 1, true)

do while NOT objStream.AtEndOfStream
strLine = objStream.ReadLine
do while len(strLine)
boolValidNumeral = true
strWord = left(strLine, inStr(strLine, " "))
for x = 1 to len(strWord)
chrChar = mid(strWord, x, 1)
if (inStr(strValidChars, chrChar) = 0) then boolValidNumeral = false
next
if (boolValidNumeral) then doStuff()
strLine = right(strLine, len(strLine) - len(strWord))
loop
loop

It's not clean but it should get the job done (unless this is not at all what you wanted to do). Also, this is untested code.


-Geates
 
Use the Split() function, using the example above you can split strLine into words:
strWord=Split(strLine," ")
and then
If strWord(0)="OFFSET" then
'strWord(1) is first number, strWord(2) is second number...
'Do your calculations
end if
 
Personally, I would split() on a line by line basis. It makes things a lot simpler but I got heat for suggesting using split() in another post so I tried to avoid it this time.

With split, the code becomes.

strValidChars = "0123456789+-.,"
set objStream = objFSO.OpenTextFile("file.ext", 1, true)

do while NOT objStream.AtEndOfStream
strLine = objStream.ReadLine
arrWords = split(strLine, " ")
for each strWord in arrWords
boolValidNumeral = true
for x = 1 to len(strWord)
chrChar = mid(strWord, x, 1)
if (inStr(strValidChars, chrChar) = 0) then boolValidNumeral = false
next
if (boolValidNumeral) then doStuff()
next
loop


 
What's wrong with split? If it works then use it...
Anyway you can use IsNumeric() to test for numbers and the OP stated that the values he is looking for is on teh line starting with OFFSET so you only have to test the lines starting with OFFSET.
 
Yes, you are right. I guess it would have helped to observe that condition.
 
An adaption of Gaetes routine:

Code:
dim fso, file, strLine, strWords
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.OpenTextFile("c:\temp\data.txt",1,True)

do while not file.AtEndOfStream
   strLine=file.ReadLine
   strWords=split(strLine)
   if strWords(0) = "OFFSET" then
      if IsNumeric(strWords(1)) then
         msgbox strWords(1)
      end if
   end if
loop

file.close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top