AmVerse Tip no.6 : inserting ID numbers
(and other number sequences)

TextPad offers an easy way to insert a series of incrementing numbers into a text. Suppose that you have a hundred endnotes, each of which you have tagged as a <DIV2 TYPE="note">, and to each of which you wish to add a unique ID.

All you need to do (once you have removed the carriage returns introduced by Author/Editor's export function) is search for

<DIV2 TYPE="note">

and replace (with the "regular expression" box checked) with

<DIV2 TYPE="note" ID="note\i(1,1)">

The replacement expression \i(x,y) inserts a different number into the text each time it finds a match. The first number it inserts is "x"; each subsequent number is incremented by "y". The replacement above will therefore change all the <DIV2 TYPE="note"> elements in order, like this:

<DIV2 TYPE="note" ID="note1">
<DIV2 TYPE="note" ID="note2">
<DIV2 TYPE="note" ID="note3">
<DIV2 TYPE="note" ID="note4">
<DIV2 TYPE="note" ID="note5">

etc.

You can of course also run this on a selection of text rather than on the whole document, and you can modify it to cope with other variations. For example, if you wanted to add an ID to *all* DIVs, regardless of level or type, you could replace this:

<DIV\([0-9]\) TYPE="\([^"]+\)">

with this:

<DIV\1 TYPE="\2" ID="div\i(1,1)">

This replacement would assign ID's to all the DIVs in order, like this:

<DIV1 TYPE="section" ID="div1">
 <DIV2 TYPE="part" ID="div2">
 <DIV2 TYPE="part" ID="div3">
 <DIV2 TYPE="part" ID="div4">
  <DIV3 TYPE="chapter" ID="div5">
  <DIV3 TYPE="chapter" ID="div6">
<DIV1 TYPE="section" ID="div7">

etc.

(If you'd rather use Perl, here's an example of a script that does the same thing. If you really want to go this route, let's talk.

#!\perl\bin\Perl.exe
$counter=0;
while (<>) {
s,(<DIV[1-7][^>]*?)>,qq{$1 ID=\"div}.++$counter.qq{-06122000\">},ge;
print;
}

)