[comp.lang.icon] a small problem

naz@hslrswi.hasler.ascom.ch (Norman H. Azadian) (01/09/91)

I'm new to this string scanning stuff, and I'm having difficulty finding
a nice clean way to perform the following simple calculation.

I have a string, S, in which I want to know the number of characters
which are used by any of several known substrings.  There is no problem
with overlapping substrings.

Here is a small example to show exactly what I mean:

	count := 0
	every  s ? find ("whizzy" | "ding")  do  count +:= 1

This would do what I want if I merely wanted to count the number of
occurrences of the substrings "whizzy" and "ding".  However, what I
want to do is know the total number of characters in s which are
used for these substrings, however many times they may occur.

I'm hoping to find something with an elegance approaching that of
the above example.  Brute force solutions need not apply.


P.S.  The real-life problem is that I have ANSI control sequences
embedded in a string which wants to be centered.

NHA
---
PAPER:  Norman Azadian; Hasler AG; Belpstrasse 23; 3000 Berne 14; Switzerland
X.400:  naz@hslrswi.hasler
UUCP:   ...{uunet,ukc,mcvax,...}!cernvax!hslrswi!naz
VOICE:  +41 31 63 2178            BITNET: naz%hslrswi.UUCP@cernvax.BITNET
-- 
PAPER:  Norman Azadian; Ascom AG; Belpstrasse 23; 3000 Berne 14; Switzerland
INTERNET:  naz%hslrswi.uucp@uunet.uu.net
UUCP:   ...{uunet,ukc,mcvax,...}!chx400!hslrswi!naz
VOICE:  +41 31 63 2178            BITNET: naz%hslrswi.UUCP@cernvax.BITNET

goer@quads.uchicago.edu (Richard L. Goerwitz) (01/10/91)

In <9101100454.AA01800@internal.apple.com> nevin@APPLE.COM (Nevin Liber) writes:
>
>> ...what I
>> want to do is know the total number of characters in s which are
>> used for these substrings, however many times they may occur.
>
>The following will do the job:
>
>	iCharacterCount := 0
>	every find(sSubString := !SSubStrings, sLine) do
>		iCharacterCount +:= *sSubString

This is nice.  One thing, though, might be added:  If any of the strings
in set SSubStrings overlap, then the above solution won't work the way
Norman wants it to.  The substrings variable needs to be a list, with the
elements prioritized in some way (e.g. longest strings first):

	count := 0
	s ? while tab(find(tmp := !substrings)) do
		count +:= *=tmp

I haven't tested this code fragment.  Oh no! :-)

Question:  What is the best way to take a list of strings, remove those
strings which are substrings of some other string in the list, and then
sort by length?

-Richard (goer@sophist.uchicago.edu)