Tapatalk

Perform Loop or PlayMacro a variable number of times

Perform Loop or PlayMacro a variable number of times

6
NewbieNewbie
6

    May 23, 2006#1

    Input file looks similar to:
    username1,10170100,4
    username1,10170110,4
    username1,10170120,4
    username1,10170130,4
    username2,10183700,4
    username2,10561790,3
    username2,10574010,3
    username2,10574020,3
    username2,10574030,3

    I'm trying to put all the username values on one line separated by the pipe character.

    Example:
    username1,10170100|10170110|10170120|10170130|10183700
    username2,10561790|10574010|10574020|10574030

    With the input file, I used MS Access to add a username count-1 at the end of the record to help me perform the "pipeit" macro shown below that certain number of times.

    It all works fine but I can't seem to perform a Loop, x number of times. Where x would be the Paste.

    My 3 macros look like this:
    Macro name: structure2
    InsertMode
    ColumnModeOff
    HexOff
    UnixReOff
    Key END
    StartSelect
    Key Ctrl+LEFT ARROW
    Copy
    EndSelect
    Key LEFT ARROW

    Macro name: pipeit
    InsertMode
    ColumnModeOff
    HexOff
    UnixReOff
    StartSelect
    Key Ctrl+RIGHT ARROW
    Key Ctrl+RIGHT ARROW
    Key Ctrl+RIGHT ARROW
    Key Ctrl+RIGHT ARROW
    Key Ctrl+RIGHT ARROW
    "|"
    EndSelect
    Key END
    Key Ctrl+LEFT ARROW
    Key Ctrl+LEFT ARROW

    MacroName: trimend
    InsertMode
    ColumnModeOff
    HexOff
    UnixReOff
    StartSelect
    Key END
    EndSelect
    Key DEL
    Key RIGHT ARROW

    Overall MacroName: All
    InsertMode
    ColumnModeOff
    HexOff
    UnixReOff
    PlayMacro 1 "structure2"
    Loop Paste
    PlayMacro 1 "pipeit"
    EndLoop
    PlayMacro 1 "trimend"

    Loop Paste below is always replaced with my last Copy value not the command: Paste.

    I also tried to do a PlayMacro "^c" "pipeit" but that didn't work either.

    6,685587
    Grand MasterGrand Master
    6,685587

      May 23, 2006#2

      Variables are not possible in the macro language except with ^c and ^s in Find, Replace, FindInFiles, ReplInFiles, Open and SaveAs, but definitively not in loops. However, a variable is not necessary for your task. A single macro with a single loop till end of file will do the reformat job.

      The macro below needs the macro property Continue if a Find with Replace not found enabled. The macro is designed to run on DOS terminated files.

      How it works:

      First it makes sure that the last line of your file is terminated with a CRLF which is important for this macro.

      Next it deletes with a single regular expression replace the number you added with MS Access at the end of every line. Then the loop starts.

      Inside the loop first the username is selected by selecting everything from start of the line till first comma without the comma. This text is copied to the clipboard. After moving the cursor once right it deletes the just copied username in all lines below the current line. So every line with the current username now starts with a comma.

      Next the cursor is moved to end of the current line (first line of current user). From this position it selects with a find everything till first line which does not start with a comma (= line with next username). There is hopefully no username which starts with a comma! If this find does not select everything, the macro has reached the last user and everything till end of the file must be selected.

      Next every line termination followed by a comma is replaced by the pipe character in the selected area only. The reformating of the current user is finished. Unselect the selection and redo this procedure at the next line if end of file is not reached.

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Bottom
      IfColNum 1
      Else
      "
      "
      EndIf
      Top
      Find RegExp ",[~ ,^p]+$"
      Replace All ""

      Clipboard 9
      Loop
      IfEof
      ExitLoop
      EndIf
      Find RegExp "%[~,^p]+"
      Copy
      EndSelect
      Key RIGHT ARROW
      Find "^c"
      Replace All ""
      Key END
      StartSelect
      Find RegExp "%[~,]"
      IfSel
      Key LEFT ARROW
      Find SelectText "^p,"
      Replace All SelectText "|"
      Else
      EndSelect
      SelectToBottom
      Find "^p,"
      Replace All "|"
      EndIf
      EndSelect
      Key UP ARROW
      Key DOWN ARROW
      EndLoop
      ClearClipboard
      Clipboard 0

      Well, this macro can be more simplified because the block selecting is not really needed. But the simplified version below could be need more execution time if your data file is really large.

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Bottom
      IfColNum 1
      Else
      "
      "
      EndIf
      Top
      Find RegExp ",[~ ,^p]+$"
      Replace All ""

      Clipboard 9
      Loop
      IfEof
      ExitLoop
      EndIf
      Find RegExp "%[~,^p]+"
      Copy
      EndSelect
      Key RIGHT ARROW
      Find "^c"
      Replace All ""
      Key END
      Find "^p,"
      Replace All "|"
      Key HOME
      IfColNumGt 1
      Key HOME
      EndIf
      Key DOWN ARROW
      EndLoop
      ClearClipboard
      Clipboard 0

      Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.
      Best regards from an UC/UE/UES for Windows user from Austria

      6
      NewbieNewbie
      6

        May 24, 2006#3

        The macro you sent seemed to run too fast. It seemed to strip out some of the data after the username. I tried modifying the macro many different ways today and couldn't get it to work for me. Also, I didn't see a Paste of the username in the macro after the Copy command.

        Here's a sample file I'm hoping you could take another look at. I took out the username counters.

        andli1,10561790
        andli1,10574010
        andli1,10574020
        andli1,10574030
        angeu,12175000
        angeu,12175050
        arkma,10387000
        armmi,13175000

        The result should look something like:
        andli1,10561790|10574010|10574020|10574030
        angeu,12175000|12175050
        arkma,10387000
        armmi,13175000

        Thank you so much for your help.

        6,685587
        Grand MasterGrand Master
        6,685587

          May 24, 2006#4

          The now red marked Find and Replaces in the macros above were to delete the last (=3rd) column which you have inserted with MS Access. I already have explained it. If you now want to run the macro on your source file without the 3rd column, you have to remove the red marked Find and Replace or your data will be destroyed because the numbers are deleted.

          My macros work perfect on your second example without the red Find/Replace with UE v11.20b and v12.10+1. The source file is an ASCII file with DOS terminations and not an Unicode file or a file with a different line termination.

          If my explanation was not enough to understand what's going on, do following:

          1) Insert command ExitMacro before first Find command.

          2) Run the macro once and look what has changed, what is selected and where the cursor currently is.

          3) Next use File - Revert to Saved to restore the file content.

          4) Edit the macro code again by cutting the ExitMacro command and pasting it before the next (second) Find command in the file.

          5) Do steps 2) - 4) again until you have understand step by step what the macro does.

          6
          NewbieNewbie
          6

            May 24, 2006#5

            I just upgraded to 12.10+2 from 10.20c and I'm still having problems. I could use help in two areas on this. 1. Where are there Find expression definitions? ie: %, ~, +, +$, etc. ? 2. Some of my usernames have a number after the first 5 characters. Example: davje may stand for Jeff Davidson and davje2 may stand for Jeffrey Davis. The macro is deleting the username with a numeric value after it if the first 5 characters are the same; leaving the first column something like: 2,10184700 instead of davje2,10184700.
            FYI - my file is about 5300 lines long.
            I so appreciate any help with this. And thank you for the past help. :D

            6,685587
            Grand MasterGrand Master
            6,685587

              May 25, 2006#6

              Help about the Find command can be found in the help of UE by simply entering "Find command" in the Index tab. Help about Regular Expressions can be found in the help of UE by simply entering "Regular Expressions" in the Index tab. And the help has also a search tab.

              Well, a macro can only be as good as the examples available at developing. I did not know that you have usernames which are substrings of other names.

              Here are the 2 macros again with a "match whole username only" workaround. Usage of a regular expression with ^c is quite dangerous because it will not produce the result you expect if one of your usernames contains regular expression characters. I have explained in an other forum topic how to use ^c and ^s with all possible find options/settings and what must be taken into consideration when using it in a regular expression which of course is only possible with UltraEdit style regex.

              The new code for "match whole username only" and 2 new commas are marked blue. If you have usernames with identical characters but with different case, add find parameter MatchCase also to the Find "^c" command (result is: Find MatchCase "^c").

              InsertMode
              ColumnModeOff
              HexOff
              UnixReOff
              Bottom
              IfColNum 1
              Else
              "
              "
              EndIf
              Top
              Find RegExp "%^([~^p]^)"
              Replace All "START_OF_LINE>>^1"

              Clipboard 9
              Loop
              IfEof
              ExitLoop
              EndIf
              Find RegExp "%[~,^p]+,"
              Copy
              EndSelect
              Key RIGHT ARROW
              Find "^c"
              Replace All ","
              Key END
              StartSelect
              Find RegExp "%[~,]"
              IfSel
              Key LEFT ARROW
              Find "^p,"
              Replace All SelectText "|"
              Else
              EndSelect
              SelectToBottom
              Find "^p,"
              Replace All "|"
              EndIf
              EndSelect
              Key UP ARROW
              Key DOWN ARROW
              EndLoop
              Top
              Find MatchCase "START_OF_LINE>>"
              Replace All ""

              ClearClipboard
              Clipboard 0


              And the not so complicated macro which is slower on very large files.

              InsertMode
              ColumnModeOff
              HexOff
              UnixReOff
              Bottom
              IfColNum 1
              Else
              "
              "
              EndIf
              Top
              Find RegExp "%^([~^p]^)"
              Replace All "START_OF_LINE>>^1"

              Clipboard 9
              Loop
              IfEof
              ExitLoop
              EndIf
              Find RegExp "%[~,^p]+,"
              Copy
              EndSelect
              Key RIGHT ARROW
              Find "^c"
              Replace All ","
              Key END
              Find "^p,"
              Replace All "|"
              Key HOME
              IfColNumGt 1
              Key HOME
              EndIf
              Key DOWN ARROW
              EndLoop
              Top
              Find MatchCase "START_OF_LINE>>"
              Replace All ""

              ClearClipboard
              Clipboard 0
              Best regards from an UC/UE/UES for Windows user from Austria

              6
              NewbieNewbie
              6

                May 26, 2006#7

                Thank you for your help. The macro is looking good now. :D