[comp.lang.pascal] Variable length output with Write

murphy@pur-phy (William J. Murphy) (01/13/89)

Does anyone know of a way using Write or Writeln to output a variable
length set of variables on a *single* line?  

In Fortran (Achkk Pfft..) one is allowed to imbed a loop in the
write or print statement. Print (max(i); i =1,20)) would do something
like print the first 20 elements of the array max on one line.
Can I control whether Write or Writeln print out a carriage return line
feed?

In C you could write
for(i =0; i<N; i++)
  printf("%d  ",max[i]);
printf("\n");

So, how do you do it in Pascal?

Bill Murphy
murphy@newton.physics.purdue.edu

syswerda@bbn.com (Gilbert Syswerda) (01/15/89)

In article <1779@pur-phy> murphy@newton.physics.purdue.edu (William J. Murphy) writes:
>Does anyone know of a way using Write or Writeln to output a variable
>length set of variables on a *single* line?  

>In C you could write
>for(i =0; i<N; i++)
>  printf("%d  ",max[i]);
>printf("\n");

>So, how do you do it in Pascal?

Just like in C.

for i := 0 to 19 do
  write (max[i]:1, '  ');
writeln;

dmurdoch@watdcsu.waterloo.edu (D.J. Murdoch - Statistics) (01/15/89)

In article <1779@pur-phy> murphy@newton.physics.purdue.edu (William J. Murphy) writes:
>Can I control whether Write or Writeln print out a carriage return line
>feed?

Am I missing something?  Write will only put a CR/LF in if you specifically ask
for it.  

asickels@bonnie.ics.uci.edu (Alan Sickels) (01/15/89)

In article <1779@pur-phy> murphy@newton.physics.purdue.edu (William J. Murphy)
 writes:
>Does anyone know of a way using Write or Writeln to output a variable
>length set of variables on a *single* line?  

Yes!  Write does not put a carriage return when it is finished.  The code
looks something like this:

  for I := 1 to 10 do
    write(A[I]);
  writeln;

which outputs the first 10 elements of array A.  If you really want to get
fancy, you can specify the width of the field the values will be printed in.

  write(A[I]:n);  (* where "n" is an integer >= 0 *)

will print the value in "n" spaces.  If the value (including room for a + 
or -) is wider than "n", then the width will be adjusted to allow it to fit.
If A is an array of reals, an additional value may be attached which specifies
how many decimal places to want to print:

  write(A[I]:n:m);  (* where n >= m + 2 and m >= 0 *)

NOTE: The compiler will let you get away with "n >= 0", but it's good practice
to leave a couple extra spaces for the decimal point and leading 0.

Alan Sickels

C:DOS - C:DOS:RUN - RUN:DOS:RUN - RUN:RUN:RUN

diamond@csl.sony.JUNET (Norman Diamond) (01/15/89)

In article <1779@pur-phy>, murphy@pur-phy (William J. Murphy) writes:
> Does anyone know of a way using Write or Writeln to output a variable
> length set of variables on a *single* line?  

Use Write *and* Writeln, to do exactly what they say they do.

> In C you could write
> for(i =0; i<N; i++)
>   printf("%d  ",max[i]);
> printf("\n");

In Pascal you could write
  FOR i := 0 TO n-1
  DO WRITE (max[i], '  ');
  WRITELN;
-- 
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.jp@relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-inventing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?

lowey@dvinci.USask.CA (Kevin Lowey) (01/16/89)

In article <1779@pur-phy>, murphy@pur-phy (William J. Murphy) writes:
> Does anyone know of a way using Write or Writeln to output a variable
> length set of variables on a *single* line?  

  I don't understand.  If you use WRITE, then it does not add an end of line
character.  The next write or writeln will continue on the same line, not at
the beginning of the next line.  For example:

    Write ('One, ');
    Write ('Two, ');
    write ('Three.');

  would create an output line of

   One, Two, Three.

  Is this what you want?
  
> Can I control whether Write or Writeln print out a carriage return line
> feed?

  WRITE does not put the CR/LF, writeln does.  If you want to control exactly
what characters are output, you could use the CHR function to convert an ASCII
code of a number into the actual character.  You could then get an explicit
CR/LF with the following statement:

  write (chr(13),chr(10));
  
> 
> In C you could write
> for(i =0; i<N; i++)
>   printf("%d  ",max[i]);
> printf("\n");
> 
> So, how do you do it in Pascal?

  for i := 0 to (N-1) do begin
    write (max[i],'  ');
  end;
  writeln;

-- Kevin Lowey

murphy@pur-phy (William J. Murphy) (01/16/89)

OK OK ENOUGH!!!  Thank you, I figured out how to do it a few hours after
I posted.   For some reason, I thought that write did output only to a typed
file.  Anyhow, Thanks for the suggestions.
Bill Murphy
murphy@newton.physics.purdue.edu

leonard@bucket.UUCP (Leonard Erickson) (01/17/89)

In article <1779@pur-phy> murphy@newton.physics.purdue.edu (William J. Murphy) writes:
<Does anyone know of a way using Write or Writeln to output a variable
<length set of variables on a *single* line?  
<
<In Fortran (Achkk Pfft..) one is allowed to imbed a loop in the
<write or print statement. Print (max(i); i =1,20)) would do something
<like print the first 20 elements of the array max on one line.
<Can I control whether Write or Writeln print out a carriage return line
<feed?
<
<In C you could write
<for(i =0; i<N; i++)
<  printf("%d  ",max[i]);
<printf("\n");
<
<So, how do you do it in Pascal?

FOR i := 0 to n do
  write(max[i],' ');
writeln;
 
(Yes, writeln will cheerfully accept *no* arguments! Very handy that...)

-- 
Leonard Erickson		...!tektronix!reed!percival!bucket!leonard
CIS: [70465,203]
"I used to be a hacker. Now I'm a 'microcomputer specialist'.
You know... I'd rather be a hacker."