Re: number of columns



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.



.



Relevant Pages

  • Re: number of columns
    ... Find the number of sequences of non-blank characters. ... stays in that state until a nonblank is scanned. ... fsm: do forever; sb: do forever; i+=1; if i>l then leave fsm; ...
    (comp.lang.pl1)
  • Re: number of columns
    ... Find the number of sequences of non-blank characters. ... When a nonblank is encountered ... | is used to traverse s; and n is used to count nonblank sequences. ... | if i>l then leave fsm; ...
    (comp.lang.pl1)
  • re: number of columns
    ... Find the number of sequences of non-blank characters. ... > stays in that state until a nonblank is scanned. ... > is used to traverse s; and n is used to count nonblank sequences. ... > if i>l then leave fsm; ...
    (comp.lang.pl1)
  • Re: Doublewords!
    ... "the world will be changed forever" is annoying if done too ... He's having everyone rebuild their characters from scratch. ... There are still some house rules in place that ups the power ...
    (rec.games.frp.dnd)
  • Re: Yamamoto Tsunetomo
    ... because I had to leave the "Tsunetomo" in kana. ... (But I found it later when I checked on Hagakure; the characters are 'forever morning.' ...
    (sci.lang)