Re: number of columns
- From: "James J. Weinkam" <jjw@xxxxxxxxx>
- Date: Sat, 13 Aug 2005 07:53:40 GMT
robin wrote:
From: James J. Weinkam <jjw@xxxxxxxxx> Date: Friday, 12 August 2005 15:29
robin wrote:
James J. Weinkam wrote in message <7hkKe.214883$on1.78101@clgrps13>...
robin wrote:
David Frank wrote in message ...
A "Sly one" asks in comp.lang.fortran topic "number of columns" " " Given an input text file of space separated number data (of same type " to make it simple), can you think of an elegant way to extract number " of columns in a given row? (using pure Fortran, of course) "- each EOL starts a new row " - every column is separated by at least one space -- can be more " slyi.
After several replies with no working results posted (including from
poster
below), yours truly has provided not one, but 3 solutions with results. Of course you pli'ers would have difficulty in translating any of my solutions and will respond,
It's trivial in PL/I.
get edit (s)) (l); n = 1 + TALLY(TRIM(S), ' ') - TALLY(TRIM(S), ' ');
True, and it's even simpler and shorter in APL:
My point is, what does any of this prove? The answer is nothing.
The code may prove of interest to PL/I programmers.
Well, it is a novel use of the TALLY builtin function, but hardly an ideal
way
to go about solving such a problem.
Stripped of inessentials, the problem is:
An input string consists of alternating contiguous sequences of blank and non-blank characters. Find the number of sequences of non-blank characters.
The most appropriate computational model for solving such a problem is the deterministic finite automaton. Sadly, general purpose procedural languages such as PL/I have no constructions specifically designed to facilitate the implementation of finite state machines, so the programmer is on his own.
The simplest way to implement a finite state machine in PL/I is with IF and
GOTO
logic. In the problem at hand a machine with two states, sb (scanning blanks) and sn (scanning nonblanks), does the job. The machine starts in state sb and stays in that state until a nonblank is scanned. When a nonblank is
encountered
in state sb, the count of nonblank sequences is incremented by 1 and state sn
is
entered. The machine stays in state sn until a blank is incountered, at which point it returns to state sb. The machine stops when the end of the input is reached in either state. In the code fragment below, s is the source string;
i
is used to traverse s; and n is used to count nonblank sequences.
n=0; i=0; l=length(s); sb: i+=1; if i>l then goto out; if substr(s,i,1)=' ' then goto sb; n+=1; sn: i+=1; if i>l then goto out; if substr(s,i,1)~=' ' then goto sn; goto sb; out:
At this point n contains the number of nonblank sequences and each character
of
s has been examined once.
This code is about 7 times as fast as the TALLY method on short strings (30 characters) and 10 times as fast on very long strings (15000) characters.
and about 10 times longer to design and to write, and to debug.
Finite state machines for this type of problem design themselves and implementation is a piece of cake. Why do you think they are the method of choice for implementing lexical analyzers?
Observe also that if the requirement to construct a list of the nonblank sequences is added, this solution is readily adapted to that purpose where the TALLY solution would have to be abandoned.
Really?
get edit (s)) (l); if s ^= '' then do; n = 1 + TALLY(TRIM(S), ' ') - TALLY(TRIM(S), ' '); get string (s||' ') list ((a(i) do i = 1 to n)); end;
That only works if the data are suitable for list directed input. What happens if it is a series of words, or alphanumeric part codes?
For those who object to using goto, a "structured" implementation is also
possible:
n=0; i=0; l=length(s); fsm: do forever; sb: do forever; i+=1; if i>l then leave fsm; if substr(s,i,1)~=' ' then leave sb; end sb; n+=1; sn: do forever; i+=1; if i>l then leave fsm; if substr(s,i,1)=' ' then leave sn; end sn; end fsm;
Frankly I don't see that this is any clearer than the previous solution, and
it
uses 10% more cpu time, but some may prefer it.
.
- Follow-Ups:
- Re: number of columns
- From: robin
- Re: number of columns
- From: Tom Linden
- Re: number of columns
- References:
- re: number of columns
- From: robin
- re: number of columns
- Prev by Date: re: number of columns
- Next by Date: Re: number of columns
- Previous by thread: re: number of columns
- Next by thread: Re: number of columns
- Index(es):
Relevant Pages
|