This one'll have Jari busy updating his FAQ<g>. I've managed
to figure out how to delete the last character from the end of
a string without calling an external program. Along the way,
I've stumbled across a couple of bonuses that are part of the
tail-deletion process.
Bonus 1) How many charcters are there in a given string? Use
scoring along with the ?? operator. Count the number of matches
to the dot wildcard charcter.
###############
:0
* 1^1 VAR ?? .
{ }
LENGTH = $=
###############
Bonus 2) What is the last character of a string? Or more
generally the last N charcters of a string? Note the use of
the $ sign below to anchor to end-of-string...
### Last character
:0
* VAR ?? ().*\/.$
{ TAIL=$MATCH }
#################
For last 2 characters use * VAR ?? ().*\/..$
For last 5 characters use * VAR ?? ().*\/.....$
etc, etc.
Now we get to apply these formulas to strip the last charcter
off a string. It gets a bit ugly for special cases. I've
deliberately chosen a worst-case scenario.
- set VAR="Testing 012301230111"
- extract last character of VAR, in this case "1" (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
- if the last two or more characters in VAR are identical,
they all get chopped, oops
- LENGTH(HEAD) plus 1 SHOULD equal LENGTH(VAR). That is not
the case when the last 2 (or more) ending characters are
identical.
- in that case, call appendrc recursively to stick back an
appropriate number of TAIL characters.
######## Main file
VAR="Testing 012301230111"
:0
* VAR ?? ().*\/.$
{ TAIL=$MATCH
:0
*$ VAR ?? ().*\/.*[^${TAIL}]
{ HEAD=$MATCH
:0
* -1^0
* 1^1 VAR ?? .
* -1^1 HEAD ?? .
{ TOOSHORT = $=
INCLUDERC=appendrc
}
}
}
#####################
####### appendrc file
:0
* -1^0
* 1^1 VAR ?? .
* -1^1 HEAD ?? .
{
HEAD="${HEAD}${TAIL}"
INCLUDERC=appendrc
}
#####################
--
Walter Dnes (Toronto)
<waltdnes(_at_)interlog(_dot_)com>