Macro to Change Numbers to Roman Numerals?

Help with writing and playing macros

Macro to Change Numbers to Roman Numerals?

Postby PoorPatriot » Sat Apr 17, 2010 10:20 pm

Hello,

I'm trying to write a macro to change numbers to roman numerals. Ideally to search and find the word "Chapter" and replace the number [1-?] after it with "I, II, III, IV ..." So far this what I have:

Main
Code: Select all
InsertMode
ColumnModeOff
HexOff
Top
Clipboard 9
ClearClipboard
Clipboard 8
ClearClipboard
Clipboard 7
ClearClipboard
Clipboard 6
ClearClipboard
Loop
Find Select MatchCase "Chapter "
IfSel
EndSelect
Key END
Key LEFT ARROW
Key LEFT ARROW
Key LEFT ARROW
Key LEFT ARROW
IfCharIs "0123456789"
StartSelect
Key RIGHT ARROW
Clipboard 6
Copy
EndSelect
PlayMacro 1 "RomanNums4"
Else
Key RIGHT ARROW
EndIf
IfCharIs "0123456789"
StartSelect
Key RIGHT ARROW
Clipboard 7
Copy
EndSelect
PlayMacro 1 "RomanNums3"
Else
Key RIGHT ARROW
EndIf
IfCharIs "0123456789"
StartSelect
Key RIGHT ARROW
Clipboard 8
Copy
EndSelect
PlayMacro 1 "RomanNums2"
Else
Key RIGHT ARROW
EndIf
IfCharIs "0123456789"
StartSelect
Key RIGHT ARROW
Clipboard 9
Copy
EndSelect
PlayMacro 1 "RomanNums1"
EndIf
EndIf
EndSelect
Key END
DeleteToStartofLine
InsertMode
"Chapter "
Clipboard 6
Paste
ClearClipboard
Clipboard 7
Paste
ClearClipboard
Clipboard 8
Paste
ClearClipboard
Clipboard 9
Paste
ClearClipboard
Key DOWN ARROW
IfEof
ExitLoop
EndIf
EndLoop
Clipboard 9
ClearClipboard
Clipboard 8
ClearClipboard
Clipboard 7
ClearClipboard
Clipboard 6
ClearClipboard
Clipboard 0

RomanNums1
Code: Select all
NewFile
Paste
EndSelect
Key END
StartSelect
Key LEFT ARROW
OverStrikeMode
IfCharIs "1"
"I"
EndIf
IfCharIs "2"
"II"
EndIf
IfCharIs "3"
"III"
EndIf
IfCharIs "4"
"IV"
EndIf
IfCharIs "5"
"V"
EndIf
IfCharIs "6"
"VI"
EndIf
IfCharIs "7"
"VII"
EndIf
IfCharIs "8"
"VIII"
EndIf
IfCharIs "9"
"IX"
EndIf
IfCharIs "0"
ClearClipboard
EndIf
Key HOME
StartSelect
Key END
Copy
EndSelect
CloseFile NoSave

RomanNums2
Code: Select all
NewFile
Paste
EndSelect
Key END
StartSelect
Key LEFT ARROW
OverStrikeMode
IfCharIs "1"
"X"
EndIf
IfCharIs "2"
"XX"
EndIf
IfCharIs "3"
"XXX"
EndIf
IfCharIs "4"
"XL"
EndIf
IfCharIs "5"
"L"
EndIf
IfCharIs "6"
"LX"
EndIf
IfCharIs "7"
"LXX"
EndIf
IfCharIs "8"
"LXXX"
EndIf
IfCharIs "9"
"XC"
EndIf
IfCharIs "0"
ClearClipboard
EndIf
Key HOME
StartSelect
Key END
Copy
EndSelect
CloseFile NoSave

RomanNums3
Code: Select all
NewFile
Paste
EndSelect
Key END
StartSelect
Key LEFT ARROW
OverStrikeMode
IfCharIs "1"
"C"
EndIf
IfCharIs "2"
"CC"
EndIf
IfCharIs "3"
"CCC"
EndIf
IfCharIs "4"
"CD"
EndIf
IfCharIs "5"
"D"
EndIf
IfCharIs "6"
"DC"
EndIf
IfCharIs "7"
"DCC"
EndIf
IfCharIs "8"
"DCCC"
EndIf
IfCharIs "9"
"CM"
EndIf
IfCharIs "0"
ClearClipboard
EndIf
Key HOME
StartSelect
Key END
Copy
EndSelect
CloseFile NoSave

RomanNums4
Code: Select all
NewFile
Paste
EndSelect
Key END
StartSelect
Key LEFT ARROW
OverStrikeMode
IfCharIs "1"
"M"
EndIf
IfCharIs "2"
"MM"
EndIf
IfCharIs "3"
"MMM"
EndIf
IfCharIs "4"
"(IV)"
EndIf
IfCharIs "5"
"(V)"
EndIf
IfCharIs "6"
"(VI)"
EndIf
IfCharIs "7"
"(VII)"
EndIf
IfCharIs "8"
"(VIII)"
EndIf
IfCharIs "9"
"(IX)"
EndIf
IfCharIs "0"
ClearClipboard
EndIf
Key HOME
StartSelect
Key END
Copy
EndSelect
CloseFile NoSave

I works, but still adds in some numbers and botches the last chapter. Any help would be greatly appreciated.

Best regards,
PoorPatriot
PoorPatriot
Newbie
 
Posts: 1
Joined: Sat Apr 17, 2010 9:56 pm

Re: Macro to Change Numbers to Roman Numerals?

Postby jorrasdk » Sun Apr 18, 2010 5:01 am

I realize we are in the macros subforum and that you ask for help with your macro, but if UE scripts is an option for you ? (which UE version are you using) - scripts offer a more simplistic approach. Here is an example tested with UE 16.00.0.1036:

Code: Select all
// Setup some defaults for find function:
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.replaceAll=false;

// We use perl regex
UltraEdit.perlReOn();

// Start from top of document
UltraEdit.activeDocument.top();

// Select chapternumbers. The regex uses positive lookbehind for the chapter
// part which in previous UE versions has been reported as "shaky" but it is
// tested with UE 16.00.0.1036.
while (UltraEdit.activeDocument.findReplace.find("(?<=Chapter )[0-9]+")) {
   
   // for each "hit" call romanize function with the "selection" chapter number
   // and write back the roman numerals
   UltraEdit.activeDocument.write(romanize(+UltraEdit.activeDocument.selection));
}

// Use any of the "millions" of to_roman_numerals() functions on the net - just use Google.
// This one is by " Ivan -DrSlump- Montes" from
// http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter
function romanize(num) {
  var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
      roman = '',
      i;
  for ( i in lookup ) {
    while ( num >= lookup[i] ) {
      roman += i;
      num -= lookup[i];
    }
  }
  return roman;
}
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Macro to Change Numbers to Roman Numerals?

Postby Mofi » Sun Apr 18, 2010 11:32 am

My first suggestion would have been also to use a script. However, if you use an older version of UltraEdit not supporting scripts, here is a macro solution. I combined your 5 macros into a single one. The main mistake you have made is that you used IfEof as exit condition for the loop. But IfEof is only true when visible cursor is at end of file. When a find does not find anymore the searched string, the cursor position remains at current position. Therefore your main does not exit the loop after changing the last chapter number.

Code: Select all
InsertMode
ColumnModeOff
HexOff
Top
Loop
Find MatchCase "Chapter "
IfNotFound
ExitLoop
EndIf
Key END
Key LEFT ARROW
Key LEFT ARROW
Key LEFT ARROW
Key LEFT ARROW
IfCharIs "123"
DeleteToStartofLine
IfCharIs "1"
Delete
"M"
Else
IfCharIs "2"
Delete
"MM"
Else
Delete
"MMM"
EndIf
EndIf
Else
Key RIGHT ARROW
DeleteToStartofLine
EndIf
IfCharIs "123456789"
IfCharIs "1"
Delete
"C"
Else
IfCharIs "2"
Delete
"CC"
Else
IfCharIs "3"
Delete
"CCC"
Else
IfCharIs "4"
Delete
"CD"
Else
IfCharIs "5"
Delete
"D"
Else
IfCharIs "6"
Delete
"DC"
Else
IfCharIs "7"
Delete
"DCC"
Else
IfCharIs "8"
Delete
"DCCC"
Else
Delete
"CM"
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
Else
Delete
EndIf
IfCharIs "123456789"
IfCharIs "1"
Delete
"X"
Else
IfCharIs "2"
Delete
"XX"
Else
IfCharIs "3"
Delete
"XXX"
Else
IfCharIs "4"
Delete
"XL"
Else
IfCharIs "5"
Delete
"L"
Else
IfCharIs "6"
Delete
"LX"
Else
IfCharIs "7"
Delete
"LXX"
Else
IfCharIs "8"
Delete
"LXXX"
Else
Delete
"XC"
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
Else
Delete
EndIf
IfCharIs "123456789"
IfCharIs "1"
Delete
"I"
Else
IfCharIs "2"
Delete
"II"
Else
IfCharIs "3"
Delete
"III"
Else
IfCharIs "4"
Delete
"IV"
Else
IfCharIs "5"
Delete
"V"
Else
IfCharIs "6"
Delete
"VI"
Else
IfCharIs "7"
Delete
"VII"
Else
IfCharIs "8"
Delete
"VIII"
Else
Delete
"IX"
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
Else
Key RIGHT ARROW
DeleteToStartofLine
"?"
EndIf
Key HOME
"Chapter "
EndLoop

For a better understanding of the macro code, once again the same code, but this time with indentations.

Code: Select all
InsertMode
ColumnModeOff
HexOff
Top
Loop
    Find MatchCase "Chapter "
    IfNotFound
        ExitLoop
    EndIf
    Key END
    Key LEFT ARROW
    Key LEFT ARROW
    Key LEFT ARROW
    Key LEFT ARROW
    IfCharIs "123"
        DeleteToStartofLine
        IfCharIs "1"
            Delete
            "M"
        Else
            IfCharIs "2"
                Delete
                "MM"
            Else
                Delete
                "MMM"
            EndIf
        EndIf
    Else
        Key RIGHT ARROW
        DeleteToStartofLine
    EndIf
    IfCharIs "123456789"
        IfCharIs "1"
            Delete
            "C"
        Else
            IfCharIs "2"
                Delete
                "CC"
            Else
                IfCharIs "3"
                    Delete
                    "CCC"
                Else
                    IfCharIs "4"
                        Delete
                        "CD"
                    Else
                        IfCharIs "5"
                            Delete
                            "D"
                        Else
                            IfCharIs "6"
                                Delete
                                "DC"
                            Else
                                IfCharIs "7"
                                    Delete
                                    "DCC"
                                Else
                                    IfCharIs "8"
                                        Delete
                                        "DCCC"
                                    Else
                                        Delete
                                        "CM"
                                    EndIf
                                EndIf
                            EndIf
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
    Else
        Delete
    EndIf
    IfCharIs "123456789"
        IfCharIs "1"
            Delete
            "X"
        Else
            IfCharIs "2"
                Delete
                "XX"
            Else
                IfCharIs "3"
                    Delete
                    "XXX"
                Else
                    IfCharIs "4"
                        Delete
                        "XL"
                    Else
                        IfCharIs "5"
                            Delete
                            "L"
                        Else
                            IfCharIs "6"
                                Delete
                                "LX"
                            Else
                                IfCharIs "7"
                                    Delete
                                    "LXX"
                                Else
                                    IfCharIs "8"
                                        Delete
                                        "LXXX"
                                    Else
                                        Delete
                                        "XC"
                                    EndIf
                                EndIf
                            EndIf
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
    Else
        Delete
    EndIf
    IfCharIs "123456789"
        IfCharIs "1"
            Delete
            "I"
        Else
            IfCharIs "2"
                Delete
                "II"
            Else
                IfCharIs "3"
                    Delete
                    "III"
                Else
                    IfCharIs "4"
                        Delete
                        "IV"
                    Else
                        IfCharIs "5"
                            Delete
                            "V"
                        Else
                            IfCharIs "6"
                                Delete
                                "VI"
                            Else
                                IfCharIs "7"
                                    Delete
                                    "VII"
                                Else
                                    IfCharIs "8"
                                        Delete
                                        "VIII"
                                    Else
                                        Delete
                                        "IX"
                                    EndIf
                                EndIf
                            EndIf
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
    Else
        Key RIGHT ARROW
        DeleteToStartofLine
        "?"
    EndIf
    Key HOME
    "Chapter "
EndLoop
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Macros