🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.text
Article #975

Re: sorting in troff(nroff)

#975
From: William Roberts
Date: Thu, 24 Jul 1986 10:14
75 lines
3240 bytes
Summary:
Expires:
Sender:
Followup-To:
Distribution:
Xpath: ukc eagle

In article <320@cord.UUCP> miker@cord.UUCP writes:
>abstract: how can items be sorted in troff(nroff)?
>
>explanation: given the definition of the list-begin, list-item,
>       and list-end macros, how can i generate a sorted list of
>       the first arguments to the list-item macros?
>
>       e.g., given the following pseudo-troff input:
>
>list-begin
>list-item arg1
>list-item arg1
>list-item arg1
>list-end
>list-sort      \" i.e. produce a sorted list of all arg1s
>
>       what should list-item and list-sort do?

(You are joking, aren't you?)

Ignoring the fact that troff doesn't have string comparisons,
just numeric ones, what you need is a way of imitating arrays
in troff so you can do quicksort/bubblesort or whatever.

The way to imitate arrays is to use dynamically constructed
register/string names.  To mimic an array Q with 10 elements and
access Q[i] where i is a suitable numeric register, do things
like

        .nr \n(Q\ni 7           \" Q[i] := 7
        .nr \n(Q\ni \n(Q\n(j    \" Q[i] := Q[j]
        .nr \n(Q2 3             \" Q[2] := 3

or have a string array if you can find some way of comparing
the arguments. To get arrays with more than 0 elements, you
have to encode the subscript into a single character - help is
at hand in the unlikely form of the .af primitive

        .af i a

will give you a 27 element array with index letters 0,a-z
and tricky use of .ie conditionals will give you A-Z as well by
changing the format to a or A as appropriate. Note that .af
doesn't change the way you assign numbers into the register.

Control structures - I can't offhand think of a way to do
iteration directly (Exercise for the readers?) but recursion is
just macro calling and the \$n arguments really are call-by-value
local variables!  Your only problem will be avoiding macro
nesting depth limits, but you have to tackle the sorting of
strings first.

For the paper in which I first saw these crazy ideas used (to
do proper floating keeps, no -ms rubbish here!), look in
Software- Practice & Experience sometime in the past 4 years
(?) for a paper entitled

        "* On the Power of Traps and Diversions*"

Note the use of pattern matching to compensate for failing
memory.  When you have written your sort program, you might
care to tackle the aesthetically valid idea of re-writing troff
in troff......
-- 

William Roberts         ARPA: liam@cs.qmc.ac.uk  (gw: cs.ucl.edu)
Queen Mary College      UUCP: liam@qmc-cs.UUCP
LONDON, UK              Tel:  01-980 4811 ext 3900
Technical Details
Message-ID: <174@cs.qmc.ac.uk>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!mcvax!ukc!icdoc!qmc-cs!liam
References: <320@cord.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!mcvax!ukc!icdoc!qmc-cs!liam
From: liam@cs.qmc.ac.uk (William Roberts)
Newsgroups: net.text
Subject: Re: sorting in troff(nroff)
Message-ID: <174@cs.qmc.ac.uk>
Date: Thu, 24-Jul-86 06:14:03 EDT
Article-I.D.: cs.174
Posted: Thu Jul 24 06:14:03 1986
Date-Received: Fri, 25-Jul-86 21:56:39 EDT
References: <320@cord.UUCP>
Reply-To: liam@cs.qmc.ac.uk (William Roberts)
Organization: CS Dept, Queen Mary College, University of London, UK.
Lines: 68
Keywords: troff, nroff, sorting, macros