Count commas

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
tcebob
Posts: 80
Joined: Fri Mar 28, 2003 1:20 am

Count commas

Post by tcebob »

The following is a font shape description for Autocad:
*042,46,ucb
2,14,8,(-7,-21),1,8,(0,21),090,8,(3,-1),01E,02D,02C,02B,01A,8,(-3,-1),2,098,1,
090,8,(3,-1),01E,02D,03C,02B,01A,8,(-3,-1),098,2,8,(21,0),14,8,(-14,-10),0

The 46 in the header represents the number of vector descriptions in the body. When creating new shapes one needs to count the commas and subtract 1. Very tedious.

Is there some method of counting the commas and placing the result in the clipboard? The closest I can get is to Find/replace , with ,. This will report the number of hits in the status bar, which beats manual counting but isn't useful in a macro.

tcebob
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

There is no way of doing this in TextPad that I am aware of. However, there is an indirect way of doing it in WildEdit. Consider this WildEdit replacement:
Find what: ((?:[^,]*,){2})|((?:[^,]*,){1})
Replace with: ?1(2,$1):?2(1,$2)

[X] Regular expression
[X] Replacement format

Options
[X] '.' does not match a newline character
This will find either ([^,]*,){2} (an expression (ending with a comma) with exactly 2 commas in it) or ([^,]*,){1} (an expression (ending with a comma) with exactly 1 comma in it). Note the use of the construct (?:...) instead of (...) at the inner level. The construct (?:...) groups the items within it into subexpressions, but does not mark them for use in the replacement expression; so they do not interfere with the subexpression numbering that we are about to use.

Note that the alternatives are ordered with the longest first.

The replacement expression uses conditional expressions. Look for Conditional expressions in the help under Reference | Replacement Format Syntax.

If there are 2 commas then subexpression 1 -- ((?:[^,]*,){2}) -- matches; so $1 is defined and the replacement expression (2,$1) is used.
If there is 1 comma then subexpression 2 -- ((?:[^,]*,){1}) -- matches; so $2 is defined and the replacement expression (1,$2) is used.

So zero,one is replaced with 1,zero,one
and zero,one,two is replaced with 2,zero,one,two
You will need to adjust the replacement expression to get the exact count you need.

This can be extended as far as you like. For example, if the maximum number of commas is 50, you can use this:
Find what: ((?:[^,]*,){50})|((?:[^,]*,){49})|...|((?:[^,]*,){1})
Replace with: ?1(50,$1):?2(49,$2):...:?50(1,$50)
But this is horrible, and I don't recommend doing it this way. I would use a script instead.
User avatar
tcebob
Posts: 80
Joined: Fri Mar 28, 2003 1:20 am

Post by tcebob »

Pretty neat; to bad it doesn't work in TP. But it gives me an idea to follow up on. Thanks,

tcebob
User avatar
tcebob
Posts: 80
Joined: Fri Mar 28, 2003 1:20 am

Post by tcebob »

Here's what I have so far. Needs more work

________
f: ([^,],) find anything but a comma followed by a comma
r; \1*\i* replace with same string plus *sequence no.*

tcebob
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

If you put a sequence number at the end of the record as well as after each comma:
Find what: ([^,],|$)
Replace with: \1*\i*

[X] Regular expression
then you can easily remove all but the last one:
Find what: \*[0-9]+\*(.)
Replace with: \1

[X] Regular expression
These assume you are using Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Why are you searching for [^,], rather than just a comma?
User avatar
tcebob
Posts: 80
Joined: Fri Mar 28, 2003 1:20 am

Post by tcebob »

Well, this is a case of evolution. I started by isolating all the commas, in hopes of counting them by a recursive binary replacement (of 16s then 8s, etc.) with specified numbers. Like this:

,,,,,,,,,,,,,,,.,,,,,,,,,,,, (27)

16:,,,,,,,,.,,,,

16:8:,,,,

16:8:4:0:0:

Not sure how I would add everything together. But then I stumbled on sequence numbers. Anyhow, after your suggestions, the only remaining problem is how to get the final number onto the clipboard. Probably put everything in a macro and use copy command.

tcebob
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

What do you want to do with the number once it's on the clipboard? If you just want to move it around the file you can do that with search and replace without using the clipboard.

For example, this finds a number surrounded by stars at the end of a line; it deletes the stars, moves the number to the beginning of the line, and inserts a comma:
Find what: ^(.*)\*([0-9]+)\*$
Replace with: \2,\1

[X] Regular expression
Adjust this to allow for (fixed numbers of) newlines and for the correct positioning of the moved number.
Post Reply