Bo Jacoby wrote:

> 1 INPUT;C$: IF C$="" THEN END
> 2 OPEN "CREDO" FOR INPUT AS 1: PRINT":";
> 3 IF EOF(1) THEN CLOSE: PRINT: GOTO 1
> 4 LINE INPUT#1,A$: B$=C$
> 5 IF A$=""THEN A%=-1 ELSE A%=ASC(A$)-48: A$=MID$(A$,2)
> 6 IF B$=""THEN B%=-1 ELSE B%=ASC(B$)-48: B$=MID$(B$,2)
> 7 IF A%<0 THEN PRINT" ";A$;: GOTO 3
> 8 IF A%=0 OR B%=0 OR A%=B% THEN 5 ELSE 3

This is not Visual Basic but BASIC from almost 50 years ago.  In modern 
lingo the line numbers are essentially labels.  Since I began 
programming in BASIC (and its subsequent Microsoft variations over 
time) as a hobby about 35 years ago, I'm quite familiar with the 
language and have been working bit by bit over time on translating 
BASIC stuff into J.  Unfortunately, one of the big hurdles is file I/O 
differences between the two languages.  I haven't spent much effort 
with this aspect, since most of what I've attempted to translate are 
the main code sections of programs.  Here is a somewhat J-ish (*NOT* 
strict J) pseudocode version of the above that hopefully can help other 
J-ers to come up with solutions:

("$" means the variable contains a string of characters; "%" means the 
variable is an integer; both symbols are dropped from the variable 
names below)
_________________________________________________________________

1: c=. [keyboard input terminated by ENTER key]  NB. character input

   while. c ~: '' do.    NB. loop until ENTER key only (null input)

     non-J: prepare computer to read file "CREDO" on channel 1

     smoutput ':'  NB. terminating ";" means *WITHOUT* CRLF!

3:   if. [reached end-of-file on channel 1] do.

       non-J: close channel 1

       smoutput ''  NB. terminates displayed output with CRLF

       GOTO 1

     end.

     a=. [read in next line of (CRLF-terminated) characters in file]

     b=. c

5:   if. a = '' do.

       aflag=. _1

     else.

       if. ('0' = ({. a)) do.
         aflag=. 0
       else.
         aflag=. 1    NB. arbitrary (has to be > 0)
       end.

       a=. }. a

     end.

     if. b = '' do.

       bflag=. _1

     else.

       if. ('0' = ({. b)) do.
         bflag=. 0
       else.
         bflag=. 1    NB. arbitrary (has to be > 0)
       end.

       b=. }. b

     end.

     if. (0 > aflag) do.
       smoutput " ",a    NB. terminating ";" means *WITHOUT* CRLF!
       GOTO 3
     end.

     if. (0 = aflag) +. (0 = bflag) +. (aflag = bflag) do.
       GOTO 5
     else.
       GOTO 3
     end.

   end.
_________________________________________________________________


I don't know how to read in a line at a time in J, so, if that's 
possible, it needs to be substituted above.  My experience is just to 
read in a whole file at a time (assuming it's not too big) and process 
each line with an  i.  loop or something.

The line labeled "1:" above is essentially the  prompt  command in J.  
The remaining code is somewhat an example of the typical "spaghetti" 
code, where GOTO's abound.  I rewrote the  aflag=. asc(a)-48  line to 
reflect its intent, namely, to check for a first character of '0' 
(zero).  The original BASIC program outputs successive characters to 
the display device WITHOUT outputting a CRLF after each, which 
(unfortunately) is what the  smoutput  command does.  If J can do such 
outputting, I would be very interested in knowing how to do it!  (This 
is quite common in BASIC programs.)

Although it may not be very important after some other people's 
solutions, perhaps it'll be helpful for others who might attempt a J 
translation on their own.


Harvey

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to