Solved Parse out line from file then convert it to standard notatio

November 2, 2012 at 10:58:13
Specs: Windows 7, 4.17Ghz 8GB ram
How would I go about parsing out a line from a text file and converting it from scientific notation to standard notation

part of text file:

SX                              2.500000e-03         TYPE: Real Number          
                                                     SOURCE: Relation           
                                                     ACCESS: Locked             
                                                     DESIGNATED: NO             
                                                     DESCRIPTION:      

So it would need to find the line that starts with SX pull the number out in this case it would be 2.500000e-03 then convert it to standard notation

I would like to do this in VB

I have been following a book to teach me about this but I would like input from someone with more experience

Thank


See More: Parse out line from file then convert it to standard notatio

Report •


✔ Best Answer
November 6, 2012 at 11:25:46
I probably should tell you to use Google, but I'm just going to cut to the chase, because using anything over batch script has to be an improvement and should be encouraged. Consider the following console program.

Option Infer On
Imports System.Globalization
Module Module1
  Sub Main()
    Dim numbers As New List(Of Decimal)
    Dim tokens As String()
    Dim inFile As New IO.StreamReader("some.txt")
    Do Until inFile.EndOfStream
      tokens = inFile.ReadLine.Split({" "c, vbTab}, StringSplitOptions.RemoveEmptyEntries)
      If tokens(0) = "SX" Then
        numbers.Add([Decimal].Parse(tokens(1), NumberStyles.AllowDecimalPoint Or NumberStyles.AllowExponent))
      End If
    Loop
    numbers.ForEach(Sub(o) Console.WriteLine(o.ToString()))
    Console.WriteLine("END")
    Console.ReadKey(True)
  End Sub
End Module

How To Ask Questions The Smart Way



#1
November 2, 2012 at 12:26:37
Seems simple enough. Check to see if the line starts with "SX" (assumed: All numbers are on lines starting with "SX"). If it is, tokenize the string to get the scientific number. Once you isolate the number part of the string, pass it to Decimal.Parse along with the AllowExponent and AllowDecimalPoint flags from NumberStyles.

How To Ask Questions The Smart Way


Report •

#2
November 2, 2012 at 20:48:34
yeh, vb seems to handle the e notation. I tried this:
x="2.500000e-3"
z=cdbl(x)
msgbox z
x="2.500e4"
z=cdbl(x)
msgbox z

Report •

#3
November 3, 2012 at 06:01:57
Doubles might work and they're easier to use, but they loose accuracy the smaller the number gets. This may or may not be a factor, depending on the numbers in play.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
November 3, 2012 at 18:12:03
I figured you had a reason for the mechanisms you invoked. ;-)

Report •

#5
November 5, 2012 at 13:22:39
Sweet that did work. But how do I get just the the numbers out of a text file.

I'm not sure how to parse out of a text file in vb.

I have done it in batch but I'm trying to get away from that


Report •

#6
November 5, 2012 at 18:06:48
mostly just use "split" function to split on a delimiter into an array. Use replace to make the other delims all the same, like:
line=''thi,s is, my test ,for,parsing, text"
'replace commas with spaces
line=replace(line,","," ")
a=split(line," ")
for i=0 to ubound(a)
wscript.echo a(i)
next
'end snippet

this tries to emulate "tokens=* delims=, "
hope this helps...


Report •

#7
November 6, 2012 at 10:01:36
Sorta. let me explain how i was doing it before.

For /F "delims=" %%a in ('Findstr /B /R "SX" input.txt') Do set "String=%%a"

SET string2=%%String:~32,13%%

so what would find what the string with SX in it then convert just that line into "String"
then I would take "String" and go 32 characters forward then grab the next 12 and put them into "string2"

how would i do something similar to that in VB.NET?


Report •

#8
November 6, 2012 at 10:20:48
See, if you had just listened to me before you'd know already, or at least be significantly closer to your answer.

How To Ask Questions The Smart Way


Report •

#9
November 6, 2012 at 11:11:47
Right. But I'm not quite sure how to do that.

Like I said I'm very new to vb.net


Report •

#10
November 6, 2012 at 11:25:46
✔ Best Answer
I probably should tell you to use Google, but I'm just going to cut to the chase, because using anything over batch script has to be an improvement and should be encouraged. Consider the following console program.

Option Infer On
Imports System.Globalization
Module Module1
  Sub Main()
    Dim numbers As New List(Of Decimal)
    Dim tokens As String()
    Dim inFile As New IO.StreamReader("some.txt")
    Do Until inFile.EndOfStream
      tokens = inFile.ReadLine.Split({" "c, vbTab}, StringSplitOptions.RemoveEmptyEntries)
      If tokens(0) = "SX" Then
        numbers.Add([Decimal].Parse(tokens(1), NumberStyles.AllowDecimalPoint Or NumberStyles.AllowExponent))
      End If
    Loop
    numbers.ForEach(Sub(o) Console.WriteLine(o.ToString()))
    Console.WriteLine("END")
    Console.ReadKey(True)
  End Sub
End Module

How To Ask Questions The Smart Way


Report •

#11
November 6, 2012 at 11:45:58
when I run it i get this error "A first chance exception of type 'System.IndexOutOfRangeException' occurred in test.exe"

if did work when i only had the line mentioned in my first post.

but there are multiple of those lines that are the same but one is SX one is AZ and the other is SA


Also I did look on the internet quite a bit before I came to this form. But I'm not sure if I was searching for the right thing


Report •

#12
November 6, 2012 at 11:51:53
Yeah, you'll probably need to add error handling to it. I haven't bothered as it wasn't required for the sample data you provided. Given the error, I assume you have an "SX" line without a number immediately following it on the same line.

How To Ask Questions The Smart Way


Report •

#13
November 6, 2012 at 12:38:19
I don't think I do
Here is the whole text file:
Local Parameters
----------------

Symbolic constant               Current value        Properties                  
------------------------------- -------------------- --------------------------- 
SX                              2.500000e+00         TYPE: Real Number          
                                                     SOURCE: Relation           
                                                     ACCESS: Locked             
                                                     DESIGNATED: NO             
                                                     DESCRIPTION:               

AZ                              2.000000e-03         TYPE: Real Number          
                                                     SOURCE: Relation           
                                                     ACCESS: Locked             
                                                     DESIGNATED: NO             
                                                     DESCRIPTION:               

SA                              1.000000e+02         TYPE: Real Number          
                                                     SOURCE: Relation           
                                                     ACCESS: Locked             
                                                     DESIGNATED: NO             
                                                     DESCRIPTION:               

when I run that it highlights

If tokens(0) = "SX" Then
and says "IndexOutOfRangeException was Unhandled" in a blue popup box

Report •

#14
November 6, 2012 at 12:47:40
Ah, so you have empty lines, too.
Replace
If tokens(0) = "SX" Then

With
If tokens.Length >= 2 AndAlso tokens(0) = "SX" Then

How To Ask Questions The Smart Way


Report •

#15
November 6, 2012 at 12:57:29
There we go that worked.

Thank you very much.

Do you have a book or course that you recommend for me to learn VB.NET on my own?


Report •

#16
November 6, 2012 at 14:27:16
Nope, nor do I know why a person would voluntarily learn vb.net. C# is just as easy, its syntax is more concise, and it's more popular.

How To Ask Questions The Smart Way


Report •

#17
November 8, 2012 at 21:59:01
Ok, Yeah the main reason why I picked it is so I can tackle 2 languages but only learning one.

I need to learn VBA which I hear is very similar to VB.NET

also I found a problem with the code you gave me.
if the number is negative it crashes the program ex(-1.512460e+00)

I have looked through it and cannot find the issue


Report •

#18
November 9, 2012 at 05:24:45
I need to learn VBA which I hear is very similar to VB.NET
They're not, outside of a similar sounding name.

if the number is negative it crashes the program
Mmm, and what NumberStyles flag would you add to fix it?

How To Ask Questions The Smart Way


Report •

#19
November 20, 2012 at 09:05:24
Sorry for the late response i have been very busy here lately. But your solution worked great!

In the code you gave me is there a way to exclude a character? some of them are 80054.txt and some are 80054-X.txt could I exclude any with "-X" in it? (x is representing a number that changes)

right now I added this to the loop and it is working but i feel there is a better way to do it:

For Each file In SearchParFilesArray
            If Not file.Substring(8, 1) = "-" Then
                Main.ListBox1.Items.Add(file)
            End If

But I guess I'm wrong and should go on to learning something a bit better than vb.net.

What would you suggest?


Report •

#20
November 21, 2012 at 07:29:31
rocky3598: i feel there is a better way to do it
Consider the following program:
Option Infer On
Module Module1
  Sub Main()
    Dim files = From file 
          In System.IO.Directory.EnumerateFiles("S:\somePath", "?????.txt")
          Where Not file.Contains("-")

    files.ToList().ForEach(Sub(o) Console.WriteLine(o.ToString()))
    Console.WriteLine("END") : Console.ReadKey(True)
  End Sub
End Module

rocky3598: What would you suggest?
If you want to remain in the .Net ecosystem, C# is my preference. Its syntax was based (indirectly) on C like a number of other languages, so you might find it easier to migrate to some other language in the future.

How To Ask Questions The Smart Way


Report •

#21
November 21, 2012 at 07:39:08
Thank you for the simplified way. I knew there was a better way.

Also I am not set on .net I just want to learn a solid powerful language.


Report •

#22
November 21, 2012 at 08:00:54
To be honest, if you're even semi-serious about programming, you're going to change languages half a dozen times in your career. With that in mind, ask yourself, "What do I want to do, and in what environment?" With your answer, research your linguistic options.

Just remember no language will prevent you from writing bad code, and there's rarely only one correct method. Focus more on learning concepts, and it'll be easier to express those concepts in code regardless of language.

How To Ask Questions The Smart Way


Report •

#23
November 27, 2012 at 07:04:46
Well that defiantly gives me something to think about. I'm thinking I may continue to learn some move vb just to get the concepts down. It is way better than a batch script.
Once I know vb fairly well I will move to C++

I really appreciate all your help.


Report •


Ask Question