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

Extracting values with decimal point from a text file 2

Status
Not open for further replies.

MikeCt

Programmer
Nov 6, 2001
44
US
Hi

I trying to write a procedure that can extract numbers from a text file, then multiple that value * 2, then parse
the new value back into the text file.

Example of text file ( ORIGINAL )
N1G90G81X-5.75Y1.5Z-.25R.01F2.5
N2X-3.832Y.25
N3Y-1.916Y.35
N4X0Y.45
N5X1.916Y.55
N6X3.832Y.7546
N7X5.75Y1.345

Example of the results needed ( Modified X values)
N1G90G81X-11.5Y1.5z-.25R.01F2.5
N2X-7.664Y.25
N3X-3.832Y.35
N4X0Y.45
N5X3.832Y.55
N6X7.664Y.7546
N7X11.5Y1.345

I have no problem extracting the X values from each line
Example for line N3 X_Value = "-3.832"

The problem is I do not know how to convert this string to a value that can be multiplied * 2
The Val function will convert the string to a number but does not handle the decimal point.

Any help will be appreciated
Mike

 
The Val function will convert the string to a number but does not handle the decimal point."

I'm not sure what you mean by that.

MsgBox Val("-3.832") * 2
 
What I was leaning towards that was it's not 100% necessary to use Val() at all (the immediate window will give you the coreect result if you use Tyson's example but strip out the Val()). Here's a solution (probably overkill but does demonstrate my point), that will do it...
Code:
Public Sub FormatFile(strInFile As String, strOutFile As String)

Dim m As Variant
Dim strOutput As String

With CreateObject("VBScript.RegExp")
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = "(^.*?X)(.*?)(Y.*?$)"
    For Each m In .execute(CreateObject("Scripting.FileSystemObject").OpenTextFile(strInFile, 1, False).ReadAll)
        strOutput = strOutput & m.submatches(0) & m.submatches(1) * 2 & m.submatches(2) & vbCrLf
    Next m
    
End With

'Debug.Print strOutput

CreateObject("Scripting.FileSystemObject").OpenTextFile(strOutFile, 2, True).Write strOutput


End Sub

Private Sub Command1_Click()
FormatFile "PathToYourInputFile.txt", "PathToYourOutputFile.txt"
End Sub
Hope this helps

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
Thanks
My mistake was reading the msdn example which states

"Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized."

Then there was the example they give that leaves the decimal point out

The following returns the value 1615198:
Val(" 1615 198th Street N.E.")

obviously the answer is simple, jut use the Val function
Thanks for setting me straight.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top