Getting Information and editing it from txt file

I have a txt file that looks like this most of the time

CC
FGH
AB 123,-980
AB 234,0000
AB 259,900
GG
EN
ABS

and I use the following code to do what I want

Public Type tScaleInfo
   sScaleID As String
   iScaleX As Double
   iScaleY As Double
End Type
Public tScaleData As tScaleInfo



Sub FixData()
Dim sLineItem As String

If Dir(App.Path + "\File.txt") <> "" Then Open App.Path + "\File.txt" For Input As #1 Else Exit Sub
If Dir(App.Path + "\OutFile.txt") <> "" Then Kill App.Path + "\OutFile.txt"
Open App.Path + "\Outfile.txt" For Output As #2
tScaleData.sScaleID = ""
tScaleData.iScaleX = 0
tScaleData.iScaleY = 0
While Not EOF(1)
   Line Input #1, sLineItem
   sLineItem = Trim(sLineItem)
   If InStr(sLineItem, " ") = 0 Then
       tScaleData.sScaleID = sLineItem
       Print #2, tScaleData.sScaleID + " "
     Else
       tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
       tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
       'Modify X Param here
       tScaleData.iScaleX = tScaleData.iScaleX * 0.235
       tScaleData.iScaleY = Right(sLineItem, Len(sLineItem) - InStr(sLineItem, ","))
       'Modify Y Param here
       tScaleData.iScaleY = tScaleData.iScaleY * 0.125
       Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY)
   End If
   tScaleData.sScaleID = ""
   tScaleData.iScaleX = 0
   tScaleData.iScaleY = 0
Wend
Close #1
Close #2

End Sub


Every thing was working great until I found that sometimes my file gives me a loop and looks like this

CC
FGH
AB 123,-980
AB 234,0000
AB 259,900
GG
QR 5555,310.5465,30.5466
EN
ABS

It is only when the QR line comes up.  What I am looking at doing is adding the line
If tScaleData.sScaleID = "QR" Then do something different
My problem is I'm not sure how to how to pull out all the info on the QR line like I do for tScaleData.iScaleX and tScaleData.iScaleY

Thanks Brent
GGAARRAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnChapinCommented:
Brent,
I thinnk you would be better off cutting the input lineinto pieces, and shortening it each time.
This method is easier to follow and easier to debug (in vb6). LIke this:

  If InStr(sLineItem, " ") = 0 Then
      tScaleData.sScaleID = sLineItem
      Print #2, tScaleData.sScaleID + " "
    Else
      tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      sLineItem = Mid(sLineItem, InStr(sLineItem, " ") + 1) ' chop of the ID and the space
      tScaleData.iScaleX = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the first number
      'tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * 0.235

      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the x number
      tScaleData.iScaleY =  Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the next number
      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the next number
     
      sLineItem = Mid(sLineItem, 2, len(sLineItem)) ' grab the next number if you need it

     'Modify Y Param here
      tScaleData.iScaleY = tScaleData.iScaleY * 0.125
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY)
  End If


Need More? Let me know.
If you like the answer, accept it next month, please.
John
0
R_RajeshCommented:
Hey !   Brent,
try this

Dim a() As String
aa = "QR 5555,310.5465,30.5466"
bb = Split(aa, " ") 'bb(0) will contain "QR" bb(1) will contain the rest
a = Split(bb(1), ",")
MsgBox a(0)
MsgBox a(1)
MsgBox a(2)
0
R_RajeshCommented:
btw to know the upper and lower bounds (i.e. how many string the array contains) of the array a use

MsgBox UBound(a)
MsgBox LBound(a)
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

GGAARRAuthor Commented:
John

I think we are close
I added   tScaleData.iScaleZ but for some reason it skips the Y and it also inserts the Z into the Y spot.  Then to top it off it calculates X and Y with whatever the ratio value is.

thanks Brent

Line Input #1, sLineItem
  sLineItem = Trim(sLineItem)
  If InStr(sLineItem, " ") = 0 Then
      tScaleData.sScaleID = sLineItem
      Print #2, tScaleData.sScaleID + " "
   
    If sLineItem = "QR" Then
     tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      sLineItem = Mid(sLineItem, InStr(sLineItem, " ") + 1) ' chop of the ID and the space
      tScaleData.iScaleX = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the first number
      'tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * 0.235

      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the x number
      tScaleData.iScaleY = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1)  ' find the next number
      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the next number
     
    tScaleData.iScaleZ = Mid(sLineItem, 2, Len(sLineItem)) ' grab the next number if you need it

     'Modify Y Param here
     '  Y Param doesn't need anything done to it in a QR
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY) + "," + Str(tScaleData.iScaleZ)
  End If
  Else

       tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * ratio
      tScaleData.iScaleY = Right(sLineItem, Len(sLineItem) - InStr(sLineItem, ","))
      'Modify Y Param here
      tScaleData.iScaleY = tScaleData.iScaleY * ratio
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY)
  End If
  tScaleData.sScaleID = ""
  tScaleData.iScaleX = 0
  tScaleData.iScaleY = 0
  tScaleData.iScaleZ = 0
Wend
Close #1
Close #2
0
JohnChapinCommented:
Brent,
this doesn't work on my test;
If sLineItem = "QR" Then
So I used:
If Left(sLineItem, 2) = "QR" Then

Please change the 2 to a 1 in :

tScaleData.iScaleZ = Mid(sLineItem, 1, Len(sLineItem)) ' grab the next number if you need it

You did not show the DIM of the tScaleData so I am assuming that the members are variants or doubles,
so that there is no truncation of the numbers.

Also there is an ENDIF just before the ELSE in the middle of the code.
That does not work well in VB6, only in SQL stored procedures. That may be affecting your output.
The  [End If] before [tScaleData.sScaleID = ""] closes out the [If sLineItem = "QR" Then].

If you like my answer, please accept it next month (Monday).
John
0
GGAARRAuthor Commented:
John
Ok made the changes. yes I do have tScaleData.iScaleZ as double.  now its giving me a Wend without while error

Thanks for your time  Brent

While Not EOF(1)
  Line Input #1, sLineItem
  sLineItem = Trim(sLineItem)
  If InStr(sLineItem, " ") = 0 Then
      tScaleData.sScaleID = sLineItem
      Print #2, tScaleData.sScaleID + " "
   
    If Left(sLineItem, 2) = "QR" Then
     tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      sLineItem = Mid(sLineItem, InStr(sLineItem, " ") + 1) ' chop of the ID and the space
      tScaleData.iScaleX = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the first number
      'tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * 0.235

      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the x number
      tScaleData.iScaleY = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1)  ' find the next number
      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the next number
     
    tScaleData.iScaleZ = Mid(sLineItem, 1, Len(sLineItem)) ' grab the next number if you need it

     'Modify Y Param here
     '  Y Param doesn't need anything done to it in a QR
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY) + "," + Str(tScaleData.iScaleZ)
 
  Else

       tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * ratio
      tScaleData.iScaleY = Right(sLineItem, Len(sLineItem) - InStr(sLineItem, ","))
      'Modify Y Param here
      tScaleData.iScaleY = tScaleData.iScaleY * ratio
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY)
 
  tScaleData.sScaleID = ""
  tScaleData.iScaleX = 0
  tScaleData.iScaleY = 0
  tScaleData.iScaleZ = 0
Wend
Close #1
Close #2
0
JohnChapinCommented:
I did not mean for you to remove BOTH ENDIFs just the one above the ELSE.

You still need the one that was like this:

      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY)
  End If
  tScaleData.sScaleID = ""
  tScaleData.iScaleX = 0
  tScaleData.iScaleY = 0
  tScaleData.iScaleZ = 0
Otherwise there is no closing endif for the starting [  If Left(sLineItem, 2) = "QR" Then ]

Add the endif in and then the While will see the wend.
John
0
GGAARRAuthor Commented:
still the same error.  I guess I'm just having a bad day. I can't seem to find it.

Thanks Brent


'Modify Y Param here
     '  Y Param doesn't need anything done to it in a QR
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY) + "," + Str(tScaleData.iScaleZ)

  Else

       tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * ratio
      tScaleData.iScaleY = Right(sLineItem, Len(sLineItem) - InStr(sLineItem, ","))
      'Modify Y Param here
      tScaleData.iScaleY = tScaleData.iScaleY * ratio
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY)
  End If
  tScaleData.sScaleID = ""
  tScaleData.iScaleX = 0
  tScaleData.iScaleY = 0
  tScaleData.iScaleZ = 0
Wend
Close #1
Close #2
0
JohnChapinCommented:
Can you post the whole sub so i can see it?
John
0
GGAARRAuthor Commented:
Sub FixData()

Dim ratio As Double


ratio = Text2.Text / Height1.Text


If Text2.Text <> Height1.Text Then
Dim sLineItem As String
Open "c:\temp\temp1.txt" For Input As #1
If Dir("c:\temp\temp2.txt") <> "" Then Kill "c:\temp\temp2.txt"
Open "c:\temp\temp2.txt" For Output As #2
tScaleData.sScaleID = ""
tScaleData.iScaleX = 0
tScaleData.iScaleY = 0
tScaleData.iScaleZ = 0
While Not EOF(1)
  Line Input #1, sLineItem
  sLineItem = Trim(sLineItem)
  If InStr(sLineItem, " ") = 0 Then
      tScaleData.sScaleID = sLineItem
      Print #2, tScaleData.sScaleID + " "
   
    If Left(sLineItem, 2) = "QR" Then
     tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      sLineItem = Mid(sLineItem, InStr(sLineItem, " ") + 1) ' chop of the ID and the space
      tScaleData.iScaleX = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the first number
      'tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * ratio

      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the x number
      tScaleData.iScaleY = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1)  ' find the next number
      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the next number
     
    tScaleData.iScaleZ = Mid(sLineItem, 1, Len(sLineItem)) ' grab the next number if you need it

     'Modify Y Param here
     '  Y Param doesn't need anything done to it in a QR
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY) + "," + Str(tScaleData.iScaleZ)

  Else

       tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      tScaleData.iScaleX = Mid(sLineItem, InStr(sLineItem, " "), InStr(sLineItem, ",") - InStr(sLineItem, " ") + 1)
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * ratio
      tScaleData.iScaleY = Right(sLineItem, Len(sLineItem) - InStr(sLineItem, ","))
      'Modify Y Param here
      tScaleData.iScaleY = tScaleData.iScaleY * ratio
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY)
  End If
  tScaleData.sScaleID = ""
  tScaleData.iScaleX = 0
  tScaleData.iScaleY = 0
  tScaleData.iScaleZ = 0
Wend
Close #1
Close #2

End Sub


Public Type tScaleInfo
  sScaleID As String
  iScaleX As Double
  iScaleY As Double
  iScaleZ As Double
End Type
Public tScaleData As tScaleInfo



Thanks John
Brent
0
GGAARRAuthor Commented:
I change and cleaned it up but it doesn't read the input line like, AB 123,-980   tScaleData.iScaleY = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the next number
gives and error


Sub FixData()

Dim ratio As Double

ratio = Text2.Text / Height1.Text



Dim sLineItem As String
Open "c:\temp\temp1.txt" For Input As #1
If Dir("c:\temp\temp2.txt") <> "" Then Kill "c:\temp\temp2.txt"
Open "c:\temp\temp2.txt" For Output As #2
tScaleData.sScaleID = ""
tScaleData.iScaleX = 0
tScaleData.iScaleY = 0
tScaleData.iScaleZ = 0
While Not EOF(1)
  Line Input #1, sLineItem
 sLineItem = Trim(sLineItem)
  If InStr(sLineItem, " ") = 0 Then
  tScaleData.sScaleID = sLineItem
  Print #2, tScaleData.sScaleID + " "
     
     
Else
   
     tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
      sLineItem = Mid(sLineItem, InStr(sLineItem, " ") + 1) ' chop of the ID and the space
      tScaleData.iScaleX = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the first number
     
 
      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the x number
      tScaleData.iScaleY = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the next number
      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the next number
     
   sLineItem = Mid(sLineItem, 1, Len(sLineItem)) ' grab the next number if you need it
 tScaleData.iScaleZ = Mid(sLineItem, 1, InStr(sLineItem, ",") + 1)
End If

     
If tScaleData.iScaleZ <> Null Then
 tScaleData.iScaleX = tScaleData.iScaleX * 0.235
 Else

     
      'Modify X Param here
      tScaleData.iScaleX = tScaleData.iScaleX * ratio
   
      'Modify Y Param here
      tScaleData.iScaleY = tScaleData.iScaleY * ratio
End If
      Print #2, tScaleData.sScaleID + " " + Str(tScaleData.iScaleX) + "," + Str(tScaleData.iScaleY) + "," + Str(tScaleData.iScaleZ)
 
  tScaleData.sScaleID = ""
  tScaleData.iScaleX = 0
  tScaleData.iScaleY = 0
  tScaleData.iScaleZ = 0
Wend
Close #1
Close #2






End Sub
0
JohnChapinCommented:
OK I'm working on it.
John
0
JohnChapinCommented:
YOu need to define your user def type this way, since integers will not hold the decimals.

Private Type aScaleData
   sScaleID As String
   dScaleX As Double
   dScaleY As Double
   dScaleZ As Double
End Type

Then this code works, which is very siilar to what you did inside the While loop:

If InStr(sLineItem, " ") = 0 Then
   tScaleData.sScaleID = sLineItem
   Debug.Print tScaleData.sScaleID + " "
Else
   tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
   sLineItem = Mid(sLineItem, InStr(sLineItem, " ") + 1) ' chop of the ID and the space
   Debug.Print sLineItem
   tScaleData.dScaleX = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the first number
   
   sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the x number
   Debug.Print sLineItem
   tScaleData.dScaleY = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the next number
   sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the next number
   Debug.Print sLineItem
   
   sLineItem = Mid(sLineItem, 1, Len(sLineItem)) ' grab the next number if you need it
   Debug.Print sLineItem
   tScaleData.dScaleZ = sLineItem
End If
If tScaleData.dScaleZ <> Null Then
   tScaleData.dScaleX = tScaleData.dScaleX * 0.235
Else
   'Modify X Param here
   tScaleData.dScaleX = tScaleData.dScaleX * ratio
   
   'Modify Y Param here
   tScaleData.dScaleY = tScaleData.dScaleY * ratio
End If
Debug.Print tScaleData.sScaleID + " " + Str(tScaleData.dScaleX) + "," + Str(tScaleData.dScaleY) + "," + Str(tScaleData.dScaleZ)

I leave today at 5 Eastern Daylight Time for the weekend.
If you have trouble, I hope you can get back to me before then.
John
0
JohnChapinCommented:
RATS, found a bug, please use this snippet instead.

If InStr(sLineItem, " ") = 0 Then
   tScaleData.sScaleID = sLineItem
   Debug.Print tScaleData.sScaleID + " "
Else
   tScaleData.sScaleID = Left(sLineItem, InStr(sLineItem, " ") - 1)
   sLineItem = Mid(sLineItem, InStr(sLineItem, " ") + 1) ' chop of the ID and the space
   Debug.Print sLineItem
   tScaleData.dScaleX = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the first number
   
   sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the x number
   Debug.Print sLineItem
   If InStr(sLineItem, ",") <> 0 Then
      tScaleData.dScaleY = Mid(sLineItem, 1, InStr(sLineItem, ",") - 1) ' find the next number
      sLineItem = Mid(sLineItem, InStr(sLineItem, ",") + 1) ' chop of the next number
      Debug.Print sLineItem
      tScaleData.dScaleZ = sLineItem
   Else
      tScaleData.dScaleY = sLineItem
   End If
   
End If
If tScaleData.dScaleZ > 0 Then
   tScaleData.dScaleX = tScaleData.dScaleX * 0.235
Else
   'Modify X Param here
   tScaleData.dScaleX = tScaleData.dScaleX * ratio
   
   'Modify Y Param here
   tScaleData.dScaleY = tScaleData.dScaleY * ratio
End If
Debug.Print tScaleData.sScaleID + " " + Str(tScaleData.dScaleX) + "," + Str(tScaleData.dScaleY) + "," + Str(tScaleData.dScaleZ)

THanks
John
0
JohnChapinCommented:
GGAARR,
How  is it going?
Did that last solution work for you ?

Need More? Let me know.
John
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GGAARRAuthor Commented:
Thanks John
Sorry about the slow reply my laptop is dead.  Thank goodness for backups.  I will try the code when I get back up and running. If I have any more questions I will repost with more points.
Thanks again for all your help
Brent
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.