Solved

Parsing/Replacing problem.. urgent

Posted on 2004-04-06
16
211 Views
Last Modified: 2010-05-02
ok, i have this text, and i need it in a certain way, and i can't figure out how to code it.. here's part of it

<Mouse Move (122, 385)><Left Button Down (122, 385)><Left Button Up (122, 385)><Mouse Move (122, 369)><Left Button Down (122, 369)><Left Button Up (122, 369)><Mouse Move (124, 355)><Left Button Down (124, 355)><Left Button Up (124, 355)><Mouse Move (124, 355)><Mouse Move (124, 351)><Mouse Move (124, 347)><Mouse Move (122, 345)><Mouse Move (122, 343)><Mouse Move (122, 341)><Left Button Down (122, 341)><Left Button Up (122, 341)><Mouse Move (122, 341)><Mouse Move (122, 339)><Mouse Move (122, 335)><Mouse Move (122, 331)><Mouse Move (122, 329)>

Ok, how i want this is like before each <Left Button Down(###, ###)> there is a <Mouse Move (###, ###)>

i want to remove all the Mouse Move ones except the ones JUST BEFORE the Left Button Down one..
so it would look like this:

<Mouse Move (122, 385)><Left Button Down (122, 385)><Left Button Up (122, 385)><Mouse Move (122, 369)><Left Button Down (122, 369)><Left Button Up (122, 369)><Mouse Move (124, 355)><Left Button Down (124, 355)><Left Button Up (124, 355)><Mouse Move (122, 341)><Left Button Down (122, 341)><Left Button Up(122, 341)

etc.. the numbers are all random and CANNOT BE CHANGED.. it's the code to a macro program i have

the reason i can't do this by hand is because it's VERY long.. each number is 3 characters long, or should be.  Thanks!
0
Comment
Question by:learning_t0_pr0gram
  • 6
  • 6
  • 2
  • +2
16 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 75 total points
ID: 10770114
I placed your test data into a file called "Input.txt" in my projects folder.  The new file, "Output.txt" is created in the projects folder as well.

Regards,

Idle_Mind

Private Sub Command1_Click()
    Dim inputString As String
   
    Dim leftBracket As Integer
    Dim leftStart As Integer
    Dim rightBracket As Integer
   
    Dim cmd As Variant
    Dim nextcmd As Variant
    Dim cmds As Integer
    Dim macroCmds As New Collection
       
    ' Read the input file into a string
    Open App.Path & "\Input.txt" For Binary As #1
    inputString = Space(LOF(1))
    Get #1, , inputString
    Close #1
   
    ' Parse out each command and store them as separate entries in a collection
    ' Each command uses a sequential key
    cmds = 0
    leftStart = 1
    Do
        leftBracket = InStr(leftStart, inputString, "<")
        rightBracket = InStr(leftBracket + 1, inputString, ">")
        If leftBracket > 0 And rightBracket > leftBracket Then
            cmds = cmds + 1
            macroCmds.Add Mid(inputString, leftBracket, rightBracket - leftBracket + 1), CStr(cmds)
            leftStart = rightBracket + 1
        Else
            Exit Do
        End If
    Loop While True
   
    ' Walk thru the commands in our collection
    ' If the current command is "Left Button Down" simply output it
    ' If the current command is "Mouse Move" then retrieve the next command and
    '     and check if it is a "Left Button Down"
    Open App.Path & "\Output.txt" For Output As #1
    cmds = 1
    For Each cmd In macroCmds
        If InStr(cmd, "Mouse Move") Then
            If cmds < macroCmds.Count Then
                nextcmd = macroCmds.Item(CStr(cmds + 1))
                If InStr(nextcmd, "Left Button Down") Then
                    Print #1, cmd;
                End If
            End If
        ElseIf InStr(cmd, "Left Button Down") Then
            Print #1, cmd;
        Else
            ' It is some other command you didn't mention....go ahead and output it
            Print #1, cmd;
        End If
        cmds = cmds + 1
    Next cmd
    Close #1
   
    MsgBox "Done Processing"
End Sub
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10770140
Private Function CleanMyString(ByVal srcString As String) As String
Dim mySplit() As String, c As Long, leftStr As String
mySplit = Split(srcString, ">", -1)
CleanMyString = mySplit(0)
For c = 1 To UBound(mySplit)
    leftStr = Left(mySplit(c), 5)
        If leftStr = "<Mous" Then
            If Not Left(mySplit(c - 1), 11) = "<Mouse Move" Then
                CleanMyString = CleanMyString & lastString & ">" & mySplit(c)
            End If
        ElseIf leftStr = "<Left" Then
            CleanMyString = CleanMyString & lastString & ">" & mySplit(c)
        End If
Next c
End Function

S
0
 
LVL 11

Expert Comment

by:YohanShminge
ID: 10770148
Dim arraything() As String
teststring = "<Mouse Move (122, 385)><Left Button Down (122, 385)><Left Button Up (122, 385)><Mouse Move (122, 369)><Left Button Down (122, 369)><Left Button Up (122, 369)><Mouse Move (124, 355)><Left Button Down (124, 355)><Left Button Up (124, 355)><Mouse Move (124, 355)><Mouse Move (124, 351)><Mouse Move (124, 347)><Mouse Move (122, 345)><Mouse Move (122, 343)><Mouse Move (122, 341)><Left Button Down (122, 341)><Left Button Up (122, 341)><Mouse Move (122, 341)><Mouse Move (122, 339)><Mouse Move (122, 335)><Mouse Move (122, 331)><Mouse Move (122, 329)>"
arraything() = Split(teststring, "><")
For i = 0 To UBound(arraything) - 1
    If InStr(arraything(i), "Mouse") > 0 Then
        If InStr(arraything(i + 1), "Button") > 0 Then _
        finalstring = finalstring + arraything(i) + "><"
    End If
    If InStr(arraything(i), "Button") > 0 Then
        finalstring = finalstring + arraything(i) + "><"
    End If
Next i
If InStr(arraything(i), "Button") > 0 Then
    finalstring = finalstring + arraything(i) + ">"
End If
finalstring = Left(finalstring, Len(finalstring) - 1)
Debug.Print finalstring
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 39

Expert Comment

by:appari
ID: 10770158
Private Sub Command1_Click()
Dim strText As String
Dim varArr
Dim i As Integer
Dim strResult As String
Dim bolBtnUp As Boolean

strText = "<Mouse Move (122, 385)><Left Button Down (122, 385)><Left Button Up (122, 385)><Mouse Move (122, 369)><Left Button Down (122, 369)><Left Button Up (122, 369)><Mouse Move (124, 355)><Left Button Down (124, 355)><Left Button Up (124, 355)><Mouse Move (124, 355)><Mouse Move (124, 351)><Mouse Move (124, 347)><Mouse Move (122, 345)><Mouse Move (122, 343)><Mouse Move (122, 341)><Left Button Down (122, 341)><Left Button Up (122, 341)><Mouse Move (122, 341)><Mouse Move (122, 339)><Mouse Move (122, 335)><Mouse Move (122, 331)><Mouse Move (122, 329)>"
varArr = Split(strText, "<")
bolBtnUp = False
For i = UBound(varArr) To LBound(varArr) Step -1
   
    If InStr(1, Trim(varArr(i)), "Mouse Move ", vbTextCompare) <> 0 Then
        If bolBtnUp Then
            strResult = "<" & varArr(i) & strResult
            Debug.Print strResult
            bolBtnUp = False
        End If
    Else
        If Trim(varArr(i)) <> "" Then strResult = "<" & varArr(i) & strResult
        Debug.Print strResult
    End If
    If InStr(1, Trim(varArr(i)), "Left Button Down ", vbTextCompare) <> 0 Then
        bolBtnUp = True
    End If
   
Next
Debug.Print strResult
End Sub
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10770173
ps. ignore the "lastString. Its there by mistake, and it does nothing :)
S
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 10770210
Ok, it works, except i've run into a problem..

there's a couple times in it where u see something like:

<Left Button Down (xxx, xxx)><Mouse Down (xxx, xxx)><Left Button Up (xxx, xxx)>

where there's one or two mouse downs inbetween the button down/up ...how would i make the code delete those?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10770233
Before we all jump in and post...who's method did you use?

Idle_Mind
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10770271
Here is one way to do it with my code:

Private Sub Command1_Click()
    Dim inputString As String
   
    Dim leftBracket As Integer
    Dim leftStart As Integer
    Dim rightBracket As Integer
   
    Dim cmd As Variant
    Dim nextcmd As Variant
    Dim cmds As Integer
    Dim macroCmds As New Collection
    Dim leftButtonDown As Boolean
       
    ' Read the input file into a string
    Open App.Path & "\Input.txt" For Binary As #1
    inputString = Space(LOF(1))
    Get #1, , inputString
    Close #1
   
    ' Parse out each command and store them as separate entries in a collection
    ' Each command uses a sequential key
    cmds = 0
    leftStart = 1
    Do
        leftBracket = InStr(leftStart, inputString, "<")
        rightBracket = InStr(leftBracket + 1, inputString, ">")
        If leftBracket > 0 And rightBracket > leftBracket Then
            cmds = cmds + 1
            macroCmds.Add Mid(inputString, leftBracket, rightBracket - leftBracket + 1), CStr(cmds)
            leftStart = rightBracket + 1
        Else
            Exit Do
        End If
    Loop While True
   
    ' Walk thru the commands in our collection
    ' If the current command is "Left Button Down" simply output it
    ' If the current command is "Mouse Move" then retrieve the next command and
    '     and check if it is a "Left Button Down"
    Open App.Path & "\Output.txt" For Output As #1
    cmds = 1
    leftButtonDown = False
    For Each cmd In macroCmds
        If InStr(cmd, "Mouse Move") Then
            If cmds < macroCmds.Count Then
                nextcmd = macroCmds.Item(CStr(cmds + 1))
                If InStr(nextcmd, "Left Button Down") Then
                    Print #1, cmd;
                End If
            End If
        ElseIf InStr(cmd, "Left Button Down") Then
            leftButtonDown = True
            Print #1, cmd;
        ElseIf InStr(cmd, "Left Button Up") Then
            leftButtonDown = False
            Print #1, cmd;
        ElseIf InStr(cmd, "Mouse Down") Then
            ' if it is not between Left Button Down and Left Button Up...output it
            If Not leftButtonDown Then
                Print #1, cmd;
            End If
        Else
            ' it's some other command you didn't mention...
            Print #1, cmd;
        End If
        cmds = cmds + 1
    Next cmd
    Close #1
   
    MsgBox "Done Processing"
End Sub
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 10770280
eek.. when i looked the only one who had posted was Idle Mind's..so i used his.. let me check if it works now
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10770465
Well, I'm out for the evening as I must get dinner started.  I will be back in late this evening or tomorrow morning.

If you have further questions perhaps one of these other fine experts can help you in the meantime.

Idle_Mind
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 10770496
ugh.. i'm very very sorry, but i just found something out that i didn't know..

Idle_Mind, i will award you points because you already answered the question correctly..

But if any of you other people can answer this, i will also give u points

it turns out the macro saves them differently like..

Type
X
Y
0
Type
X
Y
0

so like:

Move: 512
Mouse Down: 513
Mouse Up: 514

so like

<Mouse Move (255, 312)><Left Button Down (255, 310)><Left Button Up (255, 310)>

would look like

512   '<<<type = mousemove
255   '<< X first in the (xxx, yyy)
312   '<< Y second in the (xxx, yyy)
0      '<<Seperates the moves
513
255
310
0
514
255
310
0

etc, which would be inside the (xxx, xxx) ...if you could make it save like shown above, i'll definately award more points (when i get some, i currently have 0 and i don't know how to earn them)
:(
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10772049
Can you show part of the file that looks like this and how you want the file to appear instead?

Idle_Mind
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 10774164
actually, i think i'll be able to do this on my own, if not, i'll post another question about it.. thanks!!

...i would have given Shauli some points for trying to help, but i only have 75 and it isn't very much :(
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 10778305
Idle_Mind, if you still see this, could you explain some things about your code? like.. what does the Space() function do?  and what does LOF do? i know EOF is end of file, but dunno about LOF.. thanks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10778460
The Space(x) function returns a string containing "x" number spaces.

So:

    Dim s As String
    s = Space(5)

s should now be equal to "     " (a string containing five spaces).

The LOF(x) function returns the size in bytes of the open file with the handle "x".

The Get() function returns the next x bytes in the file where x is the length of the variable to store the bytes in.

So...

    Open App.Path & "\Input.txt" For Binary As #1
    inputString = Space(LOF(1))
    Get #1, , inputString
    Close #1

Opens "Input.txt" for binary input with the file handle #1.  We then make inputstring have the same number of spaces as there are characters in the file by combining the Space() and LOF() functions.  The Get() line then puts the entire file into our inputString variable because it is the same length as the file.  Finally, we close the file.

Hope that made sense.

Idle_Mind
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 10778783
yes, i understand, thanks again :D
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question