Tapatalk

Search and Replace Numeric Values of specific fields with a factor of .75

Search and Replace Numeric Values of specific fields with a factor of .75

2
NewbieNewbie
2

    Apr 15, 2014#1

    I have been reading many post one this site and I am impressed with the amount of knowledge. I am not well adversed in scripting and hope to find someone who can assist me with the following problem.

    In my file, I may have many lines out of thousands which contains id="ATI_. I would like to have each line selected. Within that selected line there will be the following fields which is randomly placed within that line.

    x="24.85" y="44.06" width="64.96" height="11.67"

    The numberic value can range from 1 to 9999 with either one or two decimal places. Each one of these four values needs to multipled by a factor of .75 with a maximum of two decimal places.

    For example the follow line of:
    <rect id="ATI_OTHERINSTRUCT1" x="24.85" y="44.06" fill="none" stroke="#EC008C" stroke-width="0.25" width="64.96" height="11.67"/>
    would be transformed to
    <rect id="ATI_OTHERINSTRUCT1" x="18.64" y="33.05" fill="none" stroke="#EC008C" stroke-width="0.25" width="48.72" height="8.75"/>

    Another example:
    <rect id="ATI_OTHERINSTRUCT2" x="124.5" y="144.06" height="8.6" fill="none" stroke="#EC008C" stroke-width="0.25" width="64.96"/>
    would be transformed to
    <rect id="ATI_OTHERINSTRUCT2" x="93.38" y="108.05" height="6.45" fill="none" stroke="#EC008C" stroke-width="0.25" width="48.72"/>

    width= should not be confused with stroke-width=

    I really appreciate any help that I can get with this issue.

    Thank you,

    Pete

    6,685587
    Grand MasterGrand Master
    6,685587

      Apr 16, 2014#2

      Here is the little script you asked for. It reports at end how many attribute values in how many tags were modified by the script.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
         // Define all parameter for the find used in the loop below
         // which is a case-sensitive 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;
         }
      
         var nTagCount = 0;        // Counts the tags found and modified.
         var nAttributeCount = 0;  // Counts the attributes found and modified.
      
         // Run the loop on all tags containing the string id="ATI_
         // and having also a closing angle bracket on the same line.
         while(UltraEdit.activeDocument.findReplace.find('id="ATI_*>'))
         {
            // The found string selected by UltraEdit is copied into a string variable.
            var sTag = UltraEdit.activeDocument.selection;
      
            // Find the attributes of interest with their floating point values
            // and put them into an array of strings whereby each string is
            // " name="value" (in words: space, attribute name, double quote
            // and the floating point number).
            var asAttributes = sTag.match(/ (?:width|height|x|y)=\"[\d.]+/g);
      
            // Contains the found tag at least one of the 4 attributes of interest?
            if (asAttributes != null)
            {
               var bModifiedValue = false;
               for(var nAttribute = 0; nAttribute < asAttributes.length; nAttribute++)
               {
                  // Find position of the double quote character in string.
                  var nFirstDigitPos = asAttributes[nAttribute].indexOf('"') + 1;
      
                  // The floating point value after the double quote is of interest.
                  var sFloatValue = asAttributes[nAttribute].substr(nFirstDigitPos);
      
                  // Convert the floating point string into a floating point number.
                  var fFloatValue = parseFloat(sFloatValue);
      
                  // Make sure that the string was really a valid floating point number.
                  if (!isNaN(fFloatValue))
                  {
                     fFloatValue *= 0.75;
      
                     // Get space, attribute name and double quote character as string.
                     var sModifiedAttribute = asAttributes[nAttribute].substr(0,nFirstDigitPos);
      
                     // Convert modified floating point value back to string
                     // with always two decimal places and append this value.
                     sModifiedAttribute += fFloatValue.toFixed(2);
      
                     // Replace the string (space, attribute name, double quote and
                     // floating point value) by the string with the modified value.
                     sTag = sTag.replace(asAttributes[nAttribute],sModifiedAttribute);
      
                     bModifiedValue = true;
                     nAttributeCount++;
                  }
               }
               if (bModifiedValue)  // Should be always true except all attributes
               {                    // have an invalid floating point number string.
                  // Overwrite the still selected tag by the
                  // tag string with the the modified values.
                  UltraEdit.activeDocument.write(sTag);
                  nTagCount++; // This tag was modified now.
               }
            }
         }
      
         // Set caret back to top of file.
         UltraEdit.activeDocument.top();
      
         // Build string for information about number of modified attribute values.
         var sAttributeCount = nAttributeCount.toString(10);
         sAttributeCount += " attribute value";
         if (nAttributeCount != 1) sAttributeCount += "s";
      
         // Build string for information about number of modified tags.
         var sTagCount = nTagCount.toString(10);
         sTagCount += " tag";
         if (nTagCount != 1) sTagCount += "s";
      
         // Show a message box with number of modified attributes and tags.
         UltraEdit.messageBox("Modified "+sAttributeCount+" in "+sTagCount+".");
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Apr 16, 2014#3

        Very impressive.....Thank you very much.

        Pete