## Re: [Clip] Sorting and aligning a list

Expand Messages
• ... Nothing canned but how about this: ^!Replace ^ h*? K( d+?)(?= h w) 000000000\$1 AIRSW ^!Replace ^ d*? K( d{10}(?= h w) \$1 AIRSW ^!Select ALL
Message 1 of 13 , May 21, 2013
At 5/21/2013 03:59 PM, Flo wrote:
>I'm dealing with a list like...
>
>3 Carla
>23 Bertha
>110 Anthony
>2245 Dorothy
>99 Edward
>
>The job: Sort that list on the leading numbers and make each name start at the same position. That is, the result should be...
>
>···3 Carla
>··23 Bertha
>··99 Edward
>·110 Anthony
>2245 Dorothy
>
>Has anyone got an efficient solution for that job?

^!Replace "^\h*?\K(\d+?)(?=\h\w)" >> "000000000\$1" AIRSW
^!Replace "^\d*?\K(\d{10}(?=\h\w)" >> "\$1" AIRSW
^!Select ALL
^!Toolbar "Sort Ascending"
^!Replace "^000000000([1-9])" >> " \$1" AIRSW
^!Replace "^00000000([1-9])" >> " \$1" AIRSW
^!Replace "^0000000([1-9])" >> " \$1" AIRSW
.
.
.
^!Replace "^0([1-9])" >> " \$1" AIRSW

 Basically, prepend as many leading zeros as digits in your largest
number (nine in the example for a field width of ten digits)
 Sort the doc
 Replace leading zeros with spaces

I haven't tested it but it should work. (right!)
I generalized it a bit to allow for leading white space.
The removal of the leading zeros could be put into a loop but why bother?

HTH

Art
• ... I think that can be simplified: ^!Replace ^(.*)\$ \$1 WRASTI ^!Replace ^ *?([ d]{5}) (.+?)\$ \$1 \$2 WRASTI Then sort. I expect spaces to be
Message 2 of 13 , May 22, 2013
Art Kocsis wrote:
>  Replace leading zeros with spaces

I think that can be simplified:

^!Replace "^(.*)\$" >> " \$1" WRASTI
^!Replace "^ *?([ \d]{5}) (.+?)\$" >> "\$1 \$2" WRASTI

Then sort. I expect spaces to be sorted before all digits and thus no
necessity to make them zeroes first.

Axel
• ... Yeah, I would too. But from habit and with lack of sleep for over 24 hours I didn t go that far. I also tend to be paranoid about exceptions such as
Message 3 of 13 , May 22, 2013
At 5/22/2013 04:02 AM, you wrote:
>Art Kocsis wrote:
>>  Replace leading zeros with spaces
>
>I think that can be simplified:
>
>^!Replace "^(.*)\$" >> " \$1" WRASTI
>^!Replace "^ *?([ \d]{5}) (.+?)\$" >> "\$1 \$2" WRASTI
>
>Then sort. I expect spaces to be sorted before all digits and thus no
>necessity to make them zeroes first.

Yeah, I would too. But from habit and with lack of sleep for over 24 hours I didn't go that far. I also tend to be paranoid about exceptions such as leading tabs. That could be accounted for with:

^!Replace "^\s*?(.*)\$" >> " \$1" WRASTI

So a four line, non-looping clip should satisfy Flo's criteria of "efficient", yes?

Art
• ... That s a non greedy space followed by a greedy anything. Ther latter will gobble all the spaces. In cases like this it is best to enforce a single byte
Message 4 of 13 , May 22, 2013
Art Kocsis wrote:
> ^!Replace "^\s*?(.*)\$" >> " \$1" WRASTI

That's a non greedy space followed by a greedy anything. Ther latter
will gobble all the spaces. In cases like this it is best to enforce a
single byte delimiter, thus:

^!Replace "^\s*(\d.*)\$" >> " \$1" WRASTI

Axel
• ... What are you trying to match with ^ h*? - in the beginning, all lines start with a number. ... Syntax error - closing bracket missing after {10} . If
Message 5 of 13 , May 22, 2013
--- In ntb-clips@yahoogroups.com, Art Kocsis <artkns@...> wrote:
>

>
> ^!Replace "^\h*?\K(\d+?)(?=\h\w)" >> "000000000\$1" AIRSW
> ^!Replace "^\d*?\K(\d{10}(?=\h\w)" >> "\$1" AIRSW
> ^!Select ALL
> ^!Toolbar "Sort Ascending"
> ^!Replace "^000000000([1-9])" >> " \$1" AIRSW
> ^!Replace "^00000000([1-9])" >> " \$1" AIRSW
> ^!Replace "^0000000([1-9])" >> " \$1" AIRSW
> .
> .
> ^!Replace "^0([1-9])" >> " \$1" AIRSW

Thanks for your proposal! But let me point out some inconsistencies in your clip:

> ^!Replace "^\h*?\K(\d+?)(?=\h\w)" >> "000000000\$1" AIRSW

What are you trying to match with '^\h*?'- in the beginning, all lines start with a number.

> ^!Replace "^\d*?\K(\d{10}(?=\h\w)" >> "\$1" AIRSW

Syntax error - closing bracket missing after '{10}'. If corrected, it replaces 10 digits with the same 10 digits - that has no effect.

> ^!Replace "^000000000([1-9])" >> " \$1" AIRSW
> ^!Replace "^00000000([1-9])" >> " \$1" AIRSW

Can't work - you've replaced all zeros with spaces already

Even when making this work -- in the end, the lines will be (nearly) the same as before because there is no command that will stop removing one space after the other as soon as the clip has achieved the intended alignment.

Regards,
Flo
• ... Thanks, Axel. That s working almost perfectly. However, I think we could improve it a bit. Here s the solution I ve been playing with so far: ^!Replace ^
Message 6 of 13 , May 22, 2013
--- In ntb-clips@yahoogroups.com, Axel Berger <Axel-Berger@...> wrote:
>
> I think that can be simplified:
>
> ^!Replace "^(.*)\$" >> " \$1" WRASTI
> ^!Replace "^ *?([ \d]{5}) (.+?)\$" >> "\$1 \$2" WRASTI
>
> Then sort...

Thanks, Axel. That's working almost perfectly.

However, I think we could improve it a bit. Here's the solution I've been playing with so far:

^!Replace "^" >> "\x20\x20\x20\x20" WARS
^!Replace "^\x20+?(.{3}\d\b)" >> "\$1" WARS
^!Select All
^\$StrSort("^\$GetSelection\$";0;1;0)\$

The advantage is that it doesn't store the names and needs no back reference to the names. So the engine also needs less steps to achieve a match.

In my view, there is still a problem with both clips: They don't work with numbers of ANY length. What's still missing is an easy instruction that will automatically adapt the clip to the highest number.

Of course, this could be added with some more command lines -- but I'm still curious to see a solution that matches numbers of any length "at one go"...

Flo
• ... Mine does. It selects exactly five spaces or digits after an indefinite number of spaces before and followed by a single space. It s untested but ought to
Message 7 of 13 , May 22, 2013
"flo.gehrke" wrote:
> They don't work with numbers of ANY length.

Mine does. It selects exactly five spaces or digits after an indefinite
number of spaces before and followed by a single space. It's untested
but ought to work.

Axel
• ... Sorry, I can t confirm this. Test your clip against... 3 Carla 23 Bertha 110 Anthony 224567 Dorothy 99 Edward and the result will be... ·····224567
Message 8 of 13 , May 22, 2013
--- In ntb-clips@yahoogroups.com, Axel Berger <Axel-Berger@...> wrote:
>
> "flo.gehrke" wrote:
> > They don't work with numbers of ANY length.
>
> Mine does. It selects exactly five spaces or digits after an
> indefinite number of spaces before and followed by a single
> space.

Sorry, I can't confirm this. Test your clip against...

3 Carla
23 Bertha
110 Anthony
224567 Dorothy
99 Edward

and the result will be...

·····224567 Dorothy
····3 Carla
···23 Bertha
···99 Edward
··110 Anthony

No left alignment, wrong sorting.

> It's untested but ought to work.

Couldn't we agree on a Clip List Rule #1:

"Don't post anything to the List that hasn't been tested before!"

Regards,
Flo
• Excellent Art. Even survives Yahoo mail!   Gesendet: Mittwoch, 22. Mai 2013 um 02:23 Uhr Von:  Art Kocsis An: NoteTab-Clips
Message 9 of 13 , May 22, 2013
Excellent Art. Even survives Yahoo mail!

Gesendet: Mittwoch, 22. Mai 2013 um 02:23 Uhr
Von: "Art Kocsis" <artkns@...>
An: NoteTab-Clips <ntb-clips@yahoogroups.com>
Betreff: Re: [Clip] Sorting and aligning a list

At 5/21/2013 03:59 PM, Flo wrote:
>I'm dealing with a list like...
>
>3 Carla
>23 Bertha
>110 Anthony
>2245 Dorothy
>99 Edward
>
>The job: Sort that list on the leading numbers and make each name start at the same position. That is, the result should be...
>
>···3 Carla
>··23 Bertha
>··99 Edward
>·110 Anthony
>2245 Dorothy
>
>Has anyone got an efficient solution for that job?

^!Replace "^\h*?\K(\d+?)(?=\h\w)" >> "000000000\$1" AIRSW
^!Replace "^\d*?\K(\d{10}(?=\h\w)" >> "\$1" AIRSW
^!Select ALL
^!Toolbar "Sort Ascending"
^!Replace "^000000000([1-9])" >> " \$1" AIRSW
^!Replace "^00000000([1-9])" >> " \$1" AIRSW
^!Replace "^0000000([1-9])" >> " \$1" AIRSW
.
.
.
^!Replace "^0([1-9])" >> " \$1" AIRSW

• Basically, prepend as many leading zeros as digits in your largest
number (nine in the example for a field width of ten digits)
• Sort the doc
• Replace leading zeros with spaces

I haven't tested it but it should work. (right!)
I generalized it a bit to allow for leading white space.
The removal of the leading zeros could be put into a loop but why bother?

HTH

Art

• ... I should have said any up to five as per the example we were given. As the number of spaces depends on the longest possible number there has to be a
Message 10 of 13 , May 22, 2013
"flo.gehrke" wrote:
> Sorry, I can't confirm this. Test your clip against...
> 224567 Dorothy

I should have said "any up to five" as per the example we were given. As
the number of spaces depends on the longest possible number there has to
be a limit, but one could loop through the data first and look for the
longest number present.

Axel
• ... Yes, I agree with you. So we have to find the longest number and to adapt the quantifiers and the spaces to that size. In this Group, we ve seen diverse
Message 11 of 13 , May 22, 2013
--- In ntb-clips@yahoogroups.com, Axel Berger <Axel-Berger@...> wrote:
>
> As the number of spaces depends on the longest possible number
> there has to be a limit, but one could loop through the data first
> and look for the longest number present.

Yes, I agree with you. So we have to find the longest number and to adapt the quantifiers and the spaces to that size.

In this Group, we've seen diverse methods of finding the longest number in a list. Here's another one: It gradually increases the quantifier which is used in a Find command. Since there is certainly no number < 3 digits, it starts with 3.

In my clip, the amount of spaces to be inserted at start of lines is equal to the length of the longest number. The quantifier to be used in the second Replace command is 'spaces -1'.

So the complete clip could look like this...

^!SetHintInfo Working...
^!SetScreenUpdate Off
^!Set %dig%=3
^!Find "^\d{^%dig%}" WRS
^!IfError Skip_2
^!Inc %dig%
^!Goto Skip_-3
^!Dec %dig%
^!Set %q%=^\$Calc(^%dig%-1)\$
^!Replace "^" >> "^\$StrFill(\x20;^%dig%)\$" WARS
^!Replace "^\x20+?(.{^%q%}\d\b)" >> "\$1" WARS
^!Select All
^\$StrSort("^\$GetSelection\$";0;1;0)\$

In my tests, this works automatically with ANY size of numbers. And it's pretty fast: Tested against 90,000 lines, the job is done in two seconds.

Regards,
Flo
• ... I guess you are having a bad day. I don t recall you being this snarky before. ... Unfortunately, by including the K the extraneous leading zeros were
Message 12 of 13 , May 23, 2013
At 5/22/2013 06:29 AM, Flo wrote:
>--- In <mailto:ntb-clips%40yahoogroups.com>ntb-clips@yahoogroups.com, Art Kocsis <artkns@...> wrote:
>> ^!Replace "^\h*?\K(\d+?)(?=\h\w)" >> "000000000\$1" AIRSW
>> ^!Replace "^\d*?\K(\d{10}(?=\h\w)" >> "\$1" AIRSW
>> ^!Select ALL
>> ^!Toolbar "Sort Ascending"
>> ^!Replace "^000000000([1-9])" >> " \$1" AIRSW
>> ^!Replace "^00000000([1-9])" >> " \$1" AIRSW
>> ^!Replace "^0000000([1-9])" >> " \$1" AIRSW
>> .
>> .
>> ^!Replace "^0([1-9])" >> " \$1" AIRSW
>
>Thanks for your proposal! But let me point out some inconsistencies in your clip:
I guess you are having a bad day. I don't recall you being this snarky before.

>> ^!Replace "^\h*?\K(\d+?)(?=\h\w)" >> "000000000\$1" AIRSW
>
>What are you trying to match with '^\h*?'- in the beginning, all lines start
>with a number.
If you had read the rest of my post I specifically said it was to generalize in case there was any leading white space and I also said it was not tested. Furthermore, I went on to detail what the clip was supposed to do so that you would be able to correct any typos or other mistakes that I might have made. In particular, I said:

> Basically, prepend as many leading zeros as digits in your largest
>number (nine in the example for a field width of ten digits)

Unfortunately, by including the "\K" the extraneous leading zeros were not discarded. That was a logic/coding mistake. Axel caught it. Apparently you did not.

>> ^!Replace "^\d*?\K(\d{10}(?=\h\w)" >> "\$1" AIRSW
>
>Syntax error - closing bracket missing after '{10}'. If corrected, it replaces 10 digits with the same 10 digits - that has no effect.
Yes, well it was supposed to have an effect and it does quite well if you remove the "\K"

^!Replace "^\h*?(\d+?)(?=\h\w)" >> "000000000\$1" AIRSW
^!Replace "^\d*?(\d{10})(?=\h\w)" >> "\$1" AIRSW

The first statement (now) discards any leading white space and prepends 9 zeros to the first digit appearing on the line and ensures a minimum field width of ten digits. The existence of at least one digit is insured by the "+" in the first replace command.

The second replace command (now) matches the ten least significant digits and discards any higher order digits (which will all be zeros). The result will be a fixed width field of ten digits (some with leading zeros), followed by a space, which can then be sorted.

>> ^!Replace "^000000000([1-9])" >> " \$1" AIRSW
>> ^!Replace "^00000000([1-9])" >> " \$1" AIRSW
>> .
>Can't work - you've replaced all zeros with spaces already
No I haven't - at least once the incorrect "\K" is removed. The first replace effects ONLY the case for nine zeros followed by a single non-zero digit, replacing the nine zeros with nine spaces. (the nine spaces apparently got lost in the yahoo translation.) This will be the condition for an original line containing only a single digit.

The second replace command matches eight zeros followed by a non-zero digit which will be the case only when there were only two digits in the original line.

Similarly for the subsequent replace commands for the remaining leading zero cases.

>Even when making this work -- in the end, the lines will be (nearly) the same as before because there is no command that will stop removing one space after the other as soon as the clip has achieved the intended alignment.
Not sure what you are saying here as there was no command posted that removed spaces so what are you wanting to stop.

Axel's clip was essentially the same except he prepended spaces directly - obviating the need to replace leading zeros with spaces.

In subsequent posts you posted your current "solution" which would handle a maximum of FOUR digits and then proceeded to berate Axel for his not working with SIX digits. Your original posted test case had four digits max, Axel's post would handle up to five digits. It's not nice to criticize someone for not satisfying a hidden agenda.

In addition, your original post asked for an "efficient" solution. It did not ask for a general solution nor did it imply anything but a one-shot need. Please be more clear and consistent when asking for help.

At 5/22/2013 07:46 AM, Flo wrote:
>Couldn't we agree on a Clip List Rule #1:
>
>"Don't post anything to the List that hasn't been tested before!"
No we cannot. It takes a lot more time to thoroughly test a clip than someone may have or be willing to spend offering assistance. When it is clearly stated that they are not tested I, for one, would much rather have those ideas, suggestions and partial solutions than none at all. In my case I had other pressing matters and did not have the time to test my suggestion (as I so stated in the post). I thought it would help you and be appreciated. I was mistaken. In the future I will withhold such assistance.
Your message has been successfully submitted and would be delivered to recipients shortly.