A correction for one more special case, a blatant typo, some
optimisation, and also clarification. I'll walk through it in
more detail.
The correction involves the fact that unlike grep, procmail's
character comparisons are *NOT* case-sensitive by default. So
the "D" flag has to be inserted in one recipe. I also had half
of an optimisation implimented, i.e. done in the main file, but
not reflected in appendrc. This time for sure...
Now we get to apply these formulas.
### The object of the following code is to strip
### off the last character of an arbitrary string.
It gets a bit ugly for special cases. I've chosen 2 scenarios
VAR="Testing abcdabcdD" and VAR="Testing abcdabcdd". Note that
the last 2 characters of the second string are identical. This
will require extra handling versus VAR="Testing abcdabcdD".
Let's do the first example.
- extract last character of VAR, in this case "D" (see above)
- assign that character to variable TAIL
- get the longest match that does not end in the TAIL
character, and store it in HEAD "Testing abcdabcd"
- LENGTH(HEAD) plus 1 SHOULD equal LENGTH(VAR).
- in this case it does, and the recipe exits without execing
the last commands
Now let's do the second example.
- extract last character of VAR, in this case "d" (see above)
- assign that character to variable TAIL
- get the longest match that does not end in the TAIL
character, and store it in HEAD "Testing abcdabc". Oops,
we took off the last *TWO* characters, but not to worry.
- LENGTH(HEAD) plus 1 SHOULD equal LENGTH(VAR).
- however, since we deleted two caharcters instead of 1,
TOOSHORT = 1, and appendrc is invoked.
- appendrc appends a copy of the TAIL character "d" and
decrements TOOSHORT, storing the result in $=. If TOOSHORT
is still positive, it assumes the decremented value, and
appendrc is invoked recursively again. This process keeps
going until TOOSHORT hits zero. At this point HEAD is
one byte shorter than VAR. The recursion and checking
allows any number of repetitions to be handled.
######## Main file
VAR="Testing abcbabcdD"
:0
* VAR ?? ()\/.$
{ TAIL=$MATCH
:0D
*$ VAR ?? ()\/.*[^${TAIL}]
{ HEAD=$MATCH
:0
* -1^0
* 1^1 VAR ?? .
* -1^1 HEAD ?? .
{ TOOSHORT = $=
INCLUDERC=appendrc
}
}
}
#####################
####### appendrc file
HEAD="${HEAD}${TAIL}"
:0
* -1^0
*$ $TOOSHORT^0
{
TOOSHORT = $=
INCLUDERC=appendrc
}
#####################
--
Walter Dnes (Toronto)
<waltdnes(_at_)interlog(_dot_)com>