Convert Roman numerals to Arabic decimal integers

Convert Roman numerals to Arabic decimal integers

17
Basic UserBasic User
17

    Nov 15, 2014#1

    Hi,

    I have a file that contains a lot of tags like below

    Code: Select all

    _tag1 text
    _tag2 some text
    _vol 1
    _iss 12
    
    _tag1 text1
    _tag2 some other text
    _vol II
    _iss 8
    
    _tag1 sample text
    _vol LXI
    _iss 8
    
    All I want is to convert the Roman numerals II and LXI in _vol tag.

    _vol contains either integer or Roman numeral only.

    Is there any way to achieve that.

    Thanks in advance.

    Arun

    6,682583
    Grand MasterGrand Master
    6,682583

      Nov 15, 2014#2

      Here is an UltraEdit script for this task using a Java function posted at Converting Roman Numerals To Decimal by Shaltiel Shmidman converted to JavaScript.

      Code: Select all

      // The function below is from http://stackoverflow.com/a/17534350/3074564 converted from Java to JavaScript.
      
      function ToArabic(sRomanNumeral)
      {
         if (!sRomanNumeral.length) return 0;
         var sFirstChar = sRomanNumeral.charAt(0);
         var sTwoChars = sRomanNumeral.substr(0,2);
         if (sFirstChar == "M") return 1000 + ToArabic(sRomanNumeral.substr(1));
         if (sTwoChars == "CM") return  900 + ToArabic(sRomanNumeral.substr(2));
         if (sFirstChar == "D") return  500 + ToArabic(sRomanNumeral.substr(1));
         if (sTwoChars == "CD") return  400 + ToArabic(sRomanNumeral.substr(2));
         if (sFirstChar == "C") return  100 + ToArabic(sRomanNumeral.substr(1));
         if (sTwoChars == "XC") return   90 + ToArabic(sRomanNumeral.substr(2));
         if (sFirstChar == "L") return   50 + ToArabic(sRomanNumeral.substr(1));
         if (sTwoChars == "XL") return   40 + ToArabic(sRomanNumeral.substr(2));
         if (sFirstChar == "X") return   10 + ToArabic(sRomanNumeral.substr(1));
         if (sTwoChars == "IX") return    9 + ToArabic(sRomanNumeral.substr(2));
         if (sFirstChar == "V") return    5 + ToArabic(sRomanNumeral.substr(1));
         if (sTwoChars == "IV") return    4 + ToArabic(sRomanNumeral.substr(2));
         if (sFirstChar == "I") return    1 + ToArabic(sRomanNumeral.substr(1));
         return 10000;
      }
      
      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Define the parameters for an UltraEdit regular expression find.
         UltraEdit.ueReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
         {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
      
         // Count the number of successful conversions for a final information message.
         var nCount = 0;
      
         // Search for "_vol" at beginning of a line with 1 or more tabs/spaces
         // and a Roman numeral to convert the Roman numeral to an Arabic numeral.
         while (UltraEdit.activeDocument.findReplace.find("%_vol[^t ]+[CDILMVX]+"))
         {
            // Get the beginning of the found string left to Roman numeral.
            var sBegin = UltraEdit.activeDocument.selection.replace(/^(_vol[\t ]+).+$/,"$1");
            // Convert the Roman numeral to an Arabic numeral.
            var nNumber = ToArabic(UltraEdit.activeDocument.selection.substr(sBegin.length));
            // Overwrite found text in file only if conversion was successful.
            if(nNumber < 10000)
            {
               UltraEdit.activeDocument.write(sBegin + nNumber.toString(10));
               nCount++;
            }
         }
         UltraEdit.activeDocument.top();
         UltraEdit.messageBox("Converted " + nCount + " Roman numeral" + ((nCount != 1) ? "s" : "") + " to decimal.");
      }
      
      Copy this code and paste it into a new ASCII file using DOS line terminators in UltraEdit.

      Next use command File - Save As to save the script file for example with name RomanToDecimal.js into %AppData%\IDMComp\UltraEdit\MyScripts or wherever you want to have saved your UltraEdit scripts.

      Open Scripting - Scripts and add the just saved UltraEdit script to the list of scripts. You can enter a description for this script, too.

      Open the file with the Roman numerals, or make this file active if it is already opened in UltraEdit.

      Run the script by clicking on it in menu Scripting, or by opening Views - Views/Lists - Script List and double clicking on the script.
      Best regards from an UC/UE/UES for Windows user from Austria

      17
      Basic UserBasic User
      17

        Nov 19, 2014#3

        Dear Mofi,

        Thanks for your reply.

        It helps me a lot.

        Just a doubt. Is there any possibilities to call or execute the script from inside the macro?

        Now I am assigned a key for script and using it.

        Thanks in advance.

        6,682583
        Grand MasterGrand Master
        6,682583

          Nov 19, 2014#4

          arunzone wrote:Is there any possibilities to call or execute the script from inside the macro?
          There is no possibility to call an UltraEdit script from an UltraEdit macro or an UltraEdit macro from an UltraEdit script. I suggest to convert the macro code to script code and do everything you want to do with the script.

          Another possibility would be using a batch file which runs first a new instance of UltraEdit with a file and a macro/script and next once more a new instance of UltraEdit with same file with script/macro. But that would be something I would never use for myself as very slow. Better would be to do everything with a script if the enhanced features of scripting engine are needed like here.
          Best regards from an UC/UE/UES for Windows user from Austria