User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
9 posts Page 1 of 1
My file contains multiple display equation tags in the form

<formula id="dqn1">....</formula> , <formula id="dqn2-6">....</formula>, <formula id="dqn7-8">....</formula>, <formula id="dqn9">....</formula> <formula id="dqn10">....</formula>

and cross-reference (link) them with

<xref ref-type="d-formula" rid="dqn1">(1)</xref>, <xref ref-type="d-formula" rid="dqn3">(3)</xref> , <xref ref-type="d-formula" rid="dqn5">(5)</xref> <xref ref-type="d-formula" rid="dqn7">(7)</xref>&#x2013;<xref ref-type="d-formula" rid="dqn8">(8)</xref>

I would like a script which modifies the above colored tags to

<xref ref-type="d-formula" rid="dqn2-6">(3)</xref>
<xref ref-type="d-formula" rid="dqn2-6">(5)</xref>
<xref ref-type="d-formula" rid="dqn7-8">(7)&#x2013;(8)</xref>

and keep the other tags intact.

How could that be done?
What is the rule which defines that

<xref ref-type="d-formula" rid="dqn3">(3)</xref>

should be modified to

<xref ref-type="d-formula" rid="dqn2-6">(3)</xref>

and

<xref ref-type="d-formula" rid="dqn5">(5)</xref>

should be also modified to

<xref ref-type="d-formula" rid="dqn2-6">(5)</xref>

That is completely unclear for me.

An UltraEdit regular expression Replace All can be used for the last two references.

Find what: ^(rid="dqn^)^([0-9]+^)^(">([0-9]+)^)</xref>&#x2013;<xref ref-type="d-formula" rid="dqn^([0-9]+^)">^(([0-9]+)</xref>^)
Replace with: ^1^2-^4^3&#x2013;^5

Same as above with Unix or Perl regular expression engine:

Find what: (rid="dqn)([0-9]+)(">\([0-9]+\))</xref>&#x2013;<xref ref-type="d-formula" rid="dqn([0-9]+)">(\([0-9]+\)</xref>)
Replace with: \1\2-\4\3&#x2013;\5
Best regards from Austria
The rule is: If there are display equation tags in the form "<formula id="dqnDIGIT1-DIGIT2">" and there are individual link ranging from the first (DIGIT) to the last (DIGIT).

If there are equations in the form say "<formula id="dqn1-4">" and there are links of equations (1), (2), (3) and (4) in the file and they are for some reason in the form

<xref ref-type="d-formula" rid="dqn1">(1)</xref>, <xref ref-type="d-formula" rid="dqn2">(2)</xref>, <xref ref-type="d-formula" rid="dqn3">(3)</xref> and <xref ref-type="d-formula" rid="dqn4">(4)</xref>

then each of rid="dqnDIGIT" portions will be changed to rid="dqn1-4" since there is no

<formula id="dqn1">, <formula id="dqn2">, ... <formula id="dqn4">

equation tag in the file because they are merged.

So the script needs to search the file for "<formula id="dqnDIGIT1-DIGIT2">" and if it finds positive results, then it will look for rid="dqnDIGIT1" and add +1 to DIGIT1 and find rid="dqnDIGIT1(+1)" until it reaches DIGIT2 and replace the links.

I hope I made a little bit clearer :|
Okay, now I have understood the rule for modification of single formula references. But there is one more case which must be taken into account.

The file contains the formulas:

  • <formula id="dqn1">....</formula>
  • <formula id="dqn2-6">....</formula>
  • <formula id="dqn7-8">....</formula>
  • <formula id="dqn9">....</formula>
  • <formula id="dqn10">....</formula>
And the file contains also the formula references

  1. <xref ref-type="d-formula" rid="dqn1">(1)</xref>
  2. <xref ref-type="d-formula" rid="dqn3">(3)</xref>
  3. <xref ref-type="d-formula" rid="dqn5">(5)</xref>
  4. <xref ref-type="d-formula" rid="dqn7">(7)</xref>&#x2013;<xref ref-type="d-formula" rid="dqn8">(8)</xref>
  5. <xref ref-type="d-formula" rid="dqn5">(5)</xref>&#x2013;<xref ref-type="d-formula" rid="dqn10">(10)</xref>
The first single reference is kept unmodified. The value of attribute rid of single references 2 and 3 can be updated. Also the fourth range reference can be updated as described.

But how to handle the fifth range reference. Should it be modified to

<xref ref-type="d-formula" rid="dqn2-6">(5)&#x2013;(10)</xref>

or should it be modified to

<xref ref-type="d-formula" rid="dqn2-6">(5)</xref>&#x2013;<xref ref-type="d-formula" rid="dqn10">(10)</xref>
Best regards from Austria
Let's say the file contains the formulas:

Code: Select all
<formula id="dqn1">....</formula>
<formula id="dqn2-6">....</formula>
<formula id="dqn7-8">....</formula>
<formula id="dqn9">....</formula>
<formula id="dqn10">....</formula>

The reference

Code: Select all
<xref ref-type="d-formula" rid="dqn2-6">(5)&#x2013;(10)</xref>

should be modified to

Code: Select all
<xref ref-type="d-formula" rid="dqn2-6">(5)</xref><xref ref-type="d-formula" rid="dqn7-8"/><xref ref-type="d-formula" rid="dqn9"/>&#x2013;<xref ref-type="d-formula" rid="dqn10">(10)</xref>

But those type of links generally do not appear in files. However, the below example does:

Code: Select all
<xref ref-type="d-formula" rid="dqn7">(7)</xref>, <xref ref-type="d-formula" rid="dqn8">(8)</xref>, <xref ref-type="d-formula" rid="dqn9">(9)</xref>&#x2013;<xref ref-type="d-formula" rid="dqn10">(10)</xref>

It should be modified to:

Code: Select all
<xref ref-type="d-formula" rid="dqn7-8">(7)&#x2013;(8)</xref>, <xref ref-type="d-formula" rid="dqn9">(9)</xref>&#x2013;<xref ref-type="d-formula" rid="dqn10">(10)</xref>

The "," and "space" between </xref> and <xref ref-type="d-formula" is also optional.
Wow, the requirements for task became more and more complex. It is really necessary to first define an input text block and a suitable output text block containing all cases which must be taken into account before coding the script can even start.

The use cases we have already found:

  1. Keep a formula reference as is.
  2. Replace just the identifier of a formula reference.
  3. Put together two or more single formula references separated by commas, whitespaces.
  4. Put together a formula reference range.
  5. Split up a formula reference range to single formula references.
  6. Split up a formula reference range to a combination of 1 or more single formula references and 1 or more formula reference ranges.
Please provide an input and a suitable example output block which can be used to test the script during development.

I think, it is best the script first loads in to memory all formula identifiers find in active file into two arrays. The first one is for formulas with an identifier in format dqnX (single formula) and the second array is for formula identifiers in format dqnX-Y (formula ranges). Next the script best expands all existing formula reference ranges to their individual formula references. And then the script can really check on reference versus formula and combine as much as possible to formula reference rages.

By the way: What to do with a formula reference for which no suitable formula identifier could be found? Should such an invalid reference be marked in any way or reported for example in output window with line number?
Best regards from Austria
Sample input:

Code: Select all
<p>A charge-coupled device <xref ref-type="d-formula" rid="dqn1">(1)</xref> comprising a plurality of rows and columns of capacitively coupled electrodes are provided in which information can be read in by applying signals to certain of the electrodes and storing them in discrete individual elements of the matrix and wherein rapid read-out may be accomplished and wherein the stored charges need not be transferred through a long line or chain of charge-coupled elements. A three-axis embodiment provides for storage elements with three mutually coupled electrodes for receiving inputs and storing information which can be read out as desired.
<formula id="dqn1">....</formula></p>
<section id="sec1">
<ti>DESCRIPTION</ti>
<p>This is a continuation of applications <xref ref-type="d-formula" rid="dqn3">(3)</xref><xref ref-type="d-formula" rid="dqn4">(4)</xref><xref ref-type="d-formula" rid="dqn5">(5)</xref> Ser. No. 297,338 filed Oct. 13, 1972, now abandoned.</p>
<section id="sec1a">
<lbl>1.</lbl>
<ti>Field of the Invention</ti>
<p>This invention relates in general to charge-coupled devices <xref ref-type="d-formula" rid="dqn7">(7)</xref>-<xref ref-type="d-formula" rid="dqn8">(8)</xref> and to memories and in particular to a novel charge-coupled memory which has fast read-in and read-out time and which is capable of storing large quantities of information.
<formula id="dqn2-6">....</formula> where <inline-eq>$M$</inline> is matter density.</p>
<p>For a simple two-electrode arrangement, depletion zones will be produced directly adjacent to the insulating layer beneath the electrodes. The fixed space charges will be positive if the semiconductor substrate is N-type semiconductor material and will be negative if the substrate is P-type semiconductor material. The space charge zone thus depends on the concentration of doping in the semiconductor and on the magnitude of the applied voltages to the electrodes. Minority charge carriers produced in the semiconductor substrate will have the same sign as the space charge and will collect at the boundary surface beneath the insulating layer. It is possible to shift this movable charge consisting of minority charges from the area below one of the electrodes to under the area below the other electrode, or, vice-versa, by means of varying the potentials which are applied to the two electrodes. Such principle is used to switch charges in the prior art devices.</p>
<p>It is an object of the present invention <xref ref-type="d-formula" rid="dqn2">(2)</xref> &#x2013; <xref ref-type="d-formula" rid="dqn5">(5)</xref> to provide an improved charge-coupled matrix which eliminates the problems of the prior art.
<formula id="dqn7-8">....</formula></p>
<p>Thus a two-dimensional matrix is formed in which binary or other intelligence can be stored and read out at a very rapid rate.</p>
<p>Minority charge carriers produced in the semiconductor member 1 will have the same charge sign as the spaced charge and will collect at the boundary surface with respect to the insulating layer 3. It is possible to shift this movable charge consisting of minority charges from the area below the electrode 4 into the area below the electrode 5 or vice-versa by controlling the potentials applied to the electrodes 4 and 5.</p>
<p>In the present invention, pairs of electrodes <xref ref-type="d-formula" rid="dqn9">(9)</xref>, <xref ref-type="d-formula" rid="dqn10">(10)</xref> are mounted on an insulating coating over a semiconductor substrate and corresponding electrodes of each pair are electrically connected together by column leads and corresponding ones of each element are connected together by row leads. Each element of the device includes at least one electrode which is separated from the semiconductor member by the insulating layer and each electrode of each group is connected to at least one other electrode of another group.
<formula id="dqn9">....</formula>.</p>
<p>The distance is determined from the equation
<formula id="dqn10">....</formula>.</p>
</section>
</section>

Desired output:

Code: Select all
<p>A charge-coupled device <xref ref-type="d-formula" rid="dqn1">(1)</xref> comprising a plurality of rows and columns of capacitively coupled electrodes are provided in which information can be read in by applying signals to certain of the electrodes and storing them in discrete individual elements of the matrix and wherein rapid read-out may be accomplished and wherein the stored charges need not be transferred through a long line or chain of charge-coupled elements. A three-axis embodiment provides for storage elements with three mutually coupled electrodes for receiving inputs and storing information which can be read out as desired.
<formula id="dqn1">....</formula></p>
<section id="sec1">
<ti>DESCRIPTION</ti>
<p>This is a continuation of applications <xref ref-type="d-formula" rid="dqn2-6">(3)</xref><xref ref-type="d-formula" rid="dqn2-6">(4)</xref><xref ref-type="d-formula" rid="dqn2-6">(5)</xref> Ser. No. 297,338 filed Oct. 13, 1972, now abandoned.</p>
<section id="sec1a">
<lbl>1.</lbl>
<ti>Field of the Invention</ti>
<p>This invention relates in general to charge-coupled devices <xref ref-type="d-formula" rid="dqn7-8">(7)&#x2013;(8)</xref> and to memories and in particular to a novel charge-coupled memory which has fast read-in and read-out time and which is capable of storing large quantities of information.
<formula id="dqn2-6">....</formula> where <inline-eq>$M$</inline> is matter density.</p>
<p>For a simple two-electrode arrangement, depletion zones will be produced directly adjacent to the insulating layer beneath the electrodes. The fixed space charges will be positive if the semiconductor substrate is N-type semiconductor material and will be negative if the substrate is P-type semiconductor material. The space charge zone thus depends on the concentration of doping in the semiconductor and on the magnitude of the applied voltages to the electrodes. Minority charge carriers produced in the semiconductor substrate will have the same sign as the space charge and will collect at the boundary surface beneath the insulating layer. It is possible to shift this movable charge consisting of minority charges from the area below one of the electrodes to under the area below the other electrode, or, vice-versa, by means of varying the potentials which are applied to the two electrodes. Such principle is used to switch charges in the prior art devices.</p>
<p>It is an object of the present invention <xref ref-type="d-formula" rid="dqn2-6">(2)&#x2013;(5)</xref> to provide an improved charge-coupled matrix which eliminates the problems of the prior art.
<formula id="dqn7-8">....</formula></p>
<p>Thus a two-dimensional matrix is formed in which binary or other intelligence can be stored and read out at a very rapid rate.</p>
<p>Minority charge carriers produced in the semiconductor member 1 will have the same charge sign as the spaced charge and will collect at the boundary surface with respect to the insulating layer 3. It is possible to shift this movable charge consisting of minority charges from the area below the electrode 4 into the area below the electrode 5 or vice-versa by controlling the potentials applied to the electrodes 4 and 5.</p>
<p>In the present invention, pairs of electrodes <xref ref-type="d-formula" rid="dqn9">(9)</xref>, <xref ref-type="d-formula" rid="dqn10">(10)</xref> are mounted on an insulating coating over a semiconductor substrate and corresponding electrodes of each pair are electrically connected together by column leads and corresponding ones of each element are connected together by row leads. Each element of the device includes at least one electrode which is separated from the semiconductor member by the insulating layer and each electrode of each group is connected to at least one other electrode of another group.
<formula id="dqn9">....</formula>.</p>
<p>The distance is determined from the equation
<formula id="dqn10">....</formula>.</p>
</section>
</section>

The script has to search only for expression "<formula id="dqnX-Y">....</formula>" and then search every rid="dqnX", rid="dqnX+1" until it reaches rid="dqnY" and replace them.

And then search for Perl expression "<xref ref-type="d-formula" rid="dqn(\d+)-(\d+)">\((\d+)\)</xref>\s*(&#x2013;|-)\s*<xref ref-type="d-formula" rid="dqn(\d+)-(\d+)">\((\d+)\)</xref>" and replace with "<xref ref-type="d-formula" rid="dqn\1-\2">\(\3\)&#x2013;\(\7\)</xref>".

All other references like:

Code: Select all
<xref ref-type="d-formula" rid="dqn1">(1)</xref>
<xref ref-type="d-formula" rid="dqn9">(9)</xref>
<xref ref-type="d-formula" rid="dqn10">(10)</xref>

should remain the same. I hope this clears all your questions. :)
I have two files say FileA and FileB.

FileA contains:

Code: Select all
rid="deqn1"<disp-formula id="deqn1-2">
rid="deqn2"<disp-formula id="deqn1-2">
rid="deqn3"<disp-formula id="deqn3-6">
rid="deqn6"<disp-formula id="deqn3-6">
rid="deqn7"<disp-formula id="deqn7-8">
rid="deqn8"<disp-formula id="deqn7-8">
rid="deqn9"<disp-formula id="deqn9-10">
rid="deqn10"<disp-formula id="deqn9-10">
rid="deqn11"<disp-formula id="deqn11-15">
rid="deqn15"<disp-formula id="deqn11-15">

And FileB (given below) contains some random stuff along will link references of each or some of the equations given in FileA.

Code: Select all
dsf fds fdfd df <xref ref-type="display-formula" rid="deqn1">(1)</xref> fds dsfds <xref ref-type="display-formula" rid="deqn2">(2)</xref>f dsfds dsf dsfds
dsafs ddsf sdfds fds  <xref ref-type="display-formula" rid="deqn17">(17)</xref> sdfds dsfds ds <xref ref-type="display-formula" rid="deqn10">(10)</xref>
<xref ref-type="display-formula" rid="deqn15">(15)</xref> sdfd dsf s <xref ref-type="display-formula" rid="deqn17">(17)</xref> dsfdfg
dsfd ds fds sdf <xref ref-type="display-formula" rid="deqn22">(22)</xref> sdfds <xref ref-type="display-formula" rid="deqn9">(9)</xref>
<xref ref-type="display-formula" rid="deqn1">(1)</xref>

I want to create a macro which first finds rid="deqnX" on FileA then goes to FileA and finds the same thing and if it finds a match, it goes back to FileA copies the correct id which are " id="deqnX-Y"" and replaces all occurrences of the said link and if it doesn't find a match then it searches the next rid="deqnX" from FileA till the end of the file. I have created a macro which needs some modification. Can anyone help?

Macro code:

Code: Select all
InsertMode
ColumnModeOff
HexOff
Loop 99999
UltraEditReOn
Find RegExp "%rid="deqn[0-9]+""
Copy
NextWindow
UltraEditReOn
Find RegExp "^c"
IfFound
PreviousWindow
UltraEditReOn
Find RegExp "id="deqn[0-9]+-[0-9]+""
Copy
NextWindow
Key RIGHT ARROW
UltraEditReOn
Find RegExp Up "rid="deqn[0-9]+""
Paste
PreviousWindow
EndIf
IfNotFound
PreviousWindow
EndIf
EndLoop
The script for How to find and replace two consecutive link separated by a comma? was not already easy to code. But the script for this task was really a hard challenge. I needed more than 15 hours to code it mainly for thinking on how to handle all those various inputs and possible combinations of cross-references and produce a suitable output.

Here is the code of the script:

Code: Select all
function outputDebugInfo(sDebugInfo)
{
//   UltraEdit.outputWindow.write(sDebugInfo);
}

if (UltraEdit.document.length > 0)  // Is any file opened?
{
   // Define environment for this script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();

   // Define from which file to read the formula identifiers.
   var oFormulaDoc = UltraEdit.activeDocument;

   var nRefIndex;
   var nFormulaIndex;
   var anFormulas = new Array();

   // Select the entire file with the formula identifiers.
   // There is nothing selected if the file is an empty file.
   oFormulaDoc.selectAll();
   if (oFormulaDoc.isSel())
   {
      // Get all formula identifiers with a single number and convert each
      // number from string to integer. The formula identifier numbers are
      // stored in a two dimensional array. On a single number first and
      // second number are identical in the two columns in the current row.
      var asFormulas = oFormulaDoc.selection.match(/<formula id="dqn\d+"/g);
      if (asFormulas != null)
      {
         for (nFormulaIndex = 0; nFormulaIndex < asFormulas.length; nFormulaIndex++)
         {
            var nFormula = parseInt(asFormulas[nFormulaIndex].replace(/^.+dqn(\d+).$/,"$1"),10);
            anFormulas.push([nFormula,nFormula]);
         }
      }

      // Get all formula identifiers with a number range and convert
      // the two numbers of each number range from string to integer.
      // The FROM number is stored in first column of formula row and
      // the TO number is stored in second column.
      var asFormulas  = oFormulaDoc.selection.match(/<formula id="dqn\d+-\d+"/g);
      if (asFormulas != null)
      {
         for (nFormulaIndex = 0; nFormulaIndex < asFormulas.length; nFormulaIndex++)
         {
            var nFrom = parseInt(asFormulas[nFormulaIndex].replace(/^.+dqn(\d+)-.+$/,"$1"),10);
            var nTo = parseInt(asFormulas[nFormulaIndex].replace(/^.+dqn\d+-(\d+).$/,"$1"),10);
            anFormulas.push([nFrom,nTo]);
         }
      }

      // Cancel the selection and move caret to top of file.
      oFormulaDoc.top();
   }

   // The formula references can be only checked and updated
   // if there was before at least 1 formula identifier found.
   if (anFormulas.length > 0)
   {
      // Dumps formular identifiers table to output window.
      // var_dump(anFormulas);

      var nRefsFound = 0;     // Counts the number of found formula reference sequences.
      var nRefsModified = 0;  // Counts the number of modified formula reference sequences.

      // Define start tag with the attributes and end tag of a formula reference.
      var sRefStart = '<xref ref-type="d-formula" rid="dqn';
      var sRefEnd   = '</xref>';

      // Define some search and replace strings used in the main loop below.
      var sFindRefs = sRefStart + '[\\d\\-]+">.+?' + sRefEnd + '(?:[\\s,-]*(?:&#x2013;\\s*)?' +
                      sRefStart + '[\\d\\-]+">.+?' + sRefEnd + ')*';
      outputDebugInfo("sFindRefs  = " + sFindRefs);

      var sReplRange = ')' + sRefEnd + '#' + sRefStart + '$1">($1';
      outputDebugInfo("sReplRange = " + sReplRange);

      var sRemoveRef = sRefStart + '\\d+">\\((\\d+)\\)' + sRefEnd;
      outputDebugInfo("sRemoveRef = " + sRemoveRef);
      var oRemoveRef = new RegExp(sRemoveRef,"g");

      // A Perl regular expression find is used to find 1 or more formula
      // references in a loop which are processed and if needed updated. The
      // formula references are searched and processed always on active file.
      UltraEdit.perlReOn();
      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;
      UltraEdit.activeDocument.findReplace.searchInColumn=false;

      UltraEdit.activeDocument.top();
      while (UltraEdit.activeDocument.findReplace.find(sFindRefs))
      {
         nRefsFound++;
         var sReferencesFound = UltraEdit.activeDocument.selection;
         outputDebugInfo("\nsReferencesFound = " + sReferencesFound);

         // Replace - and &#x2013; with or without surrounding whitespaces
         // between > and < (tags) or between ) and ( (reference numbers)
         // by a hash. Hash character is used as it does not exist anymore
         // in the reference sequence string after this replace in contrast
         // to dash character and # is no regular expression character.
         var sRefModified = sReferencesFound.replace(/([)>])\s*(?:-|&#x2013;)\s*([(<])/g,"$1#$2");

         // Remove all whitespaces and commas between the tags.
         sRefModified = sRefModified.replace(/>[\s,]+</g,"><");

         // Replace already existing rid attribute values with a range
         // by rid attribute value with a single reference number.
         sRefModified = sRefModified.replace(/\d+-\d+\">\((\d+)\)/g,'$1">($1)');

         // Convert a reference number range like (5)#(10) into a complete
         // reference range like <xref ref-type="d-formula" rid="dqn5">
         // (5)</xref>#<xref ref-type="d-formula" rid="dqn10">(10)</xref>
         sRefModified = sRefModified.replace(/\)#\((\d+)/g,sReplRange);
         outputDebugInfo("sRefModified (1) = " + sRefModified);

         // Remove the reference start and end tags to get just the reference
         // single numbers and reference number ranges separated by comma.
         sRefModified = sRefModified.replace(oRemoveRef,"$1,");

         // Insert a comma after each hash character used to mark a range.
         sRefModified = sRefModified.replace(/#/g,"#,");

         // Remove the comma at end of the string.
         sRefModified = sRefModified.substr(0,sRefModified.length-1);
         outputDebugInfo("sRefModified (2) = " + sRefModified);

         // Split up the reference numbers and reference ranges
         // for cross-checking them with the formula identifiers.
         var asReferences = sRefModified.split(",");

         // Convert the number strings into integer numbers and replace
         // also the ranges by appropriate sequences of integer numbers.
         var nRefNumber;
         var anRefNumbers = new Array();
         for (nRefIndex = 0; nRefIndex < asReferences.length; nRefIndex++)
         {
            if (asReferences[nRefIndex] != '#')
            {
               nRefNumber = parseInt(asReferences[nRefIndex],10);
               anRefNumbers.push(nRefNumber);
            }
            else
            {
               var nEndNumber = parseInt(asReferences[++nRefIndex],10);
               while (nRefNumber < nEndNumber) anRefNumbers.push(++nRefNumber);
            }
         }
         outputDebugInfo("anRefNumbers     = " + anRefNumbers.join(","));

         // Append to the array the value 0 to end any sequence always
         // with in the next loop processing the formula references.
         anRefNumbers.push(0);

         /* Rebuilt the reference(s) by putting together as much formula
            references as possible.

            The base rule for putting a sequence of references together
            is: The reference numbers are one after the other with each
            reference number being +1 of previous reference number and
            no gap between.

            There are three types of ranges:

         1. A reference number range within a single reference because the
            formula identifiers are already put together. E.g. there is

               <formula id="dqn2-6">...</formula>

            and the reference is

               <xref ref-type="d-formula" rid="dqn2-6">(3)&#x2013;(5)</xref>

            It is possible for this type of a range that end number is
            just start number plus 1, for example

            <xref ref-type="d-formula" rid="dqn2-6">(5)&#x2013;(6)</xref>

         2. A formula reference range because the formula identifiers
            are still separated. For example there are

               <formula id="dqn7">...</formula>
               <formula id="dqn8">...</formula>
               <formula id="dqn9">...</formula>

            and the matching reference range is

               <xref ref-type="d-formula" rid="dqn7">(7)</xref>
               <xref ref-type="d-formula" rid="dqn8"/>&#x2013;
               <xref ref-type="d-formula" rid="dqn9">(9)</xref>

         3. The third range type is a combination of first and second like

               <xref ref-type="d-formula" rid="dqn2-6">(3)</xref>
               <xref ref-type="d-formula" rid="dqn7"/>
               <xref ref-type="d-formula" rid="dqn8"/>&#x2013;
               <xref ref-type="d-formula" rid="dqn9">(9)</xref>

            But not put together with a dash is a sequence of exactly
            2 references with different formula identifiers for first
            and second reference like

               <formula id="dqn7">...</formula>
               <formula id="dqn8">...</formula>

            referenced with

               <xref ref-type="d-formula" rid="dqn7">(7)</xref>,
               <xref ref-type="d-formula" rid="dqn8">(8)</xref>
         */

         var nFormulaStart   = -1;
         var nFormulaEnd     = -1;
         var nReferenceStart = -1;
         var nReferenceEnd   = -1;
         var nPreviousNumber = -1;

         nRefIndex = 0;
         sRefModified = "";

         while(true)    // This loop is exited when a reference number
         {              // with the value 0 is loaded from the array.
            var bFormulaFound = false;
            nRefNumber = anRefNumbers[nRefIndex];

            if(nRefNumber > 0)
            {
               // Find the matching formula identifier for this reference.
               nFormulaIndex = 0;
               do
               {
                  if ((nRefNumber >= anFormulas[nFormulaIndex][0]) &&
                      (nRefNumber <= anFormulas[nFormulaIndex][1]))
                  {
                     bFormulaFound = true;
                     break;
                  }
               }
               while(++nFormulaIndex < anFormulas.length);
            }

            // Is there no formula matching the reference number or is the
            // current reference number not +1 of previous reference number
            // and the reference is not the first one of a possible range?
            if((!bFormulaFound || (nRefNumber != (nPreviousNumber+1))) && (nPreviousNumber > 0))
            {
               // Append single or start formula reference.
               if (nFormulaStart >= 0)
               {
                  // Append a comma and a space if the rebuilt
                  // reference string is not empty anymore.
                  if (sRefModified.length != 0) sRefModified += ", ";

                  sRefModified += sRefStart + anFormulas[nFormulaStart][0];
                  if (anFormulas[nFormulaStart][0] != anFormulas[nFormulaStart][1])
                  {
                     sRefModified += "-" + anFormulas[nFormulaStart][1];
                  }
                  sRefModified += '">(' + nReferenceStart + ')'

                  // Is there a reference range processing in progress?
                  if(nFormulaEnd >= 0)
                  {
                     // Is this a reference range of first type?
                     if (nFormulaStart == nFormulaEnd)
                     {
                        sRefModified += '&#x2013;(' + nReferenceEnd + ')';
                     }

                     // Are there only 2 references and they don't reference the
                     // same formula identifier, interpret them separated with
                     // a comma and not as formula reference range with a dash.
                     else if ((nReferenceStart+1) == nReferenceEnd)
                     {
                        sRefModified += sRefEnd + ", " + sRefStart + anFormulas[nFormulaEnd][0];
                        if (anFormulas[nFormulaEnd][0] != anFormulas[nFormulaEnd][1])
                        {
                           sRefModified += "-" + anFormulas[nFormulaEnd][1];
                        }
                        sRefModified += '">(' + nReferenceEnd + ')';
                     }

                     else  // More than 2 references with different
                     {     // start and end formula identifiers.
                        sRefModified += sRefEnd;

                        // Append the empty formula references between start and
                        // end reference number depending on existing formulas.
                        var nSequenceRef = anFormulas[nFormulaStart][1];
                        while (++nSequenceRef < anFormulas[nFormulaEnd][0])
                        {
                           for(var nSequenceIndex = 0; nSequenceIndex < anFormulas.length; nSequenceIndex++)
                           {
                              if ((nSequenceRef >= anFormulas[nSequenceIndex][0]) &&
                                  (nSequenceRef <= anFormulas[nSequenceIndex][1]))
                              {
                                 sRefModified += sRefStart + anFormulas[nSequenceIndex][0];
                                 if (anFormulas[nSequenceIndex][0] != anFormulas[nSequenceIndex][1])
                                 {
                                    nSequenceRef = anFormulas[nSequenceIndex][1];
                                    sRefModified += "-" + nSequenceRef;
                                 }
                                 sRefModified += '"/>';
                                 break;
                              }
                           }
                        }

                        // Append end formula reference.
                        sRefModified += '&#x2013;' + sRefStart + anFormulas[nFormulaEnd][0];
                        if (anFormulas[nFormulaEnd][0] != anFormulas[nFormulaEnd][1])
                        {
                           sRefModified += "-" + anFormulas[nFormulaEnd][1];
                        }
                        sRefModified += '">(' + nReferenceEnd + ')';
                     }
                     nFormulaEnd   = -1;
                     nReferenceEnd = -1;
                  }
                  nFormulaStart   = -1;
                  nReferenceStart = -1;
                  sRefModified += sRefEnd;
               }
            }

            // Are all reference numbers in array processed?
            if (nRefNumber < 1) break;

            // Is there no formula for the current formula reference number?
            if (!bFormulaFound)
            {
               // This case should never occur, but must be nevertheless
               // taken into account to avoid corruption of file content.

               // Append a comma and a space if the rebuilt
               // reference string is not empty anymore.
               if (sRefModified.length != 0) sRefModified += ", ";

               // Append a formula reference like when the formula would really exist.
               sRefModified += sRefStart + nRefNumber + '">(' + nRefNumber + ')' + sRefEnd;
               nPreviousNumber = -1;

               // Output a warning message to output window and make the output
               // window visible if not already visible on running the script.
               if (UltraEdit.outputWindow.visible == false)
               {
                  UltraEdit.outputWindow.showWindow(true);
               }
               UltraEdit.outputWindow.write("WARNING: Found no formula for formula reference " + nRefNumber);
            }
            else
            {
               // Is the current reference number +1 of previous number?
               if (++nPreviousNumber == nRefNumber)
               {
                  nReferenceEnd = nRefNumber;
                  nFormulaEnd = nFormulaIndex;
               }
               else  // First reference of a possible new range.
               {
                  nPreviousNumber = nRefNumber;
                  nReferenceStart = nRefNumber;
                  nFormulaStart = nFormulaIndex;
               }
            }
            nRefIndex++;   // Next reference from array of references.
         }

         if (sRefModified != sReferencesFound)
         {
            nRefsModified++;
            // Overwrite the found reference sequence by rebuilt string.
            UltraEdit.activeDocument.write(sRefModified);
            outputDebugInfo("sRefModified (3) = " + sRefModified);
         }
      }

      UltraEdit.activeDocument.top();
      UltraEdit.messageBox("Number of reference sequences found / modified: " + nRefsFound + " / " + nRefsModified);
   }
}

Please study the script line by line from top to bottom and ask if something is unclear. I'm quite sure this is the last script I wrote for your company which takes more than 2 hours to code. I suggest that your company hires a programmer for scripting tasks like this one. I'm sure this is a full time job in your company.

Line 5 of the output file produced from input file is different than posted above. Instead of creating

Code: Select all
<xref ref-type="d-formula" rid="dqn2-6">(3)</xref><xref ref-type="d-formula" rid="dqn2-6">(4)</xref><xref ref-type="d-formula" rid="dqn2-6">(5)</xref>

the script creates

Code: Select all
<xref ref-type="d-formula" rid="dqn2-6">(3)&#x2013;(5)</xref>

The explanation for this merging behavior can be found in the large block comment in the script which describes the 3 different types of cross-reference ranges handled by the script.

Uncomment the line in function outputDebugInfo in third line of the script if you want to see in output window what is going on script execution. You could also uncomment the line var_dump(anFormulas); to see the values of the two dimensional array which holds the identifiers from the formulas/equations as integer numbers.



In case of file with the formulas/equations (FileA) is different to the file with the formula references/links (FileB), first modify in the script the line

Code: Select all
var oFormulaDoc = UltraEdit.activeDocument;

to

Code: Select all
var oFormulaDoc = UltraEdit.document[0];

Save the script file, close it and add the script to the Script List.

Then open first FileA and second FileB and run the script from the Script List with FileB as active file.
Best regards from Austria
9 posts Page 1 of 1
cron