[GAP Forum] formatted "Print"

Alexander Konovalov alexander.konovalov at st-andrews.ac.uk
Thu Sep 1 11:38:08 BST 2016


Dear R.N.,

> On 31 Aug 2016, at 18:32, R.N. Tsai <r_n_tsai at yahoo.com> wrote:
> 
> Dear GAP forum,
> Is there a way to do a formatted print in GAP?
> Something equivalent to this for example :  %6d or %-6d  to print an alignedfixed width 6 bit integer....(these are c examples but are used in other languages too).
> Thanks,R.N.

Not that I am aware of (maybe utility function in some package though?) but you can easily achieve this using String, or PrintString or ViewString - dependently on the objects that you want to output this way. For example, this is a simple-minded attempt to add some number of spaces in front of an integer (it uses undocumented feature that ListWithIdenticalEntries returns an empty list when its 1st argument is negative):

gap> PrintFormatted:=function(d,n)
> local t,s;
> t:=String(d);
> s:=ListWithIdenticalEntries(n-Length(t),' ');
> Print(s,t);
> end;
function( d, n ) ... end

gap> for i in [1..16] do PrintFormatted(2^i,4);Print("\n");od;
   2
   4
   8
  16
  32
  64
 128
 256
 512
1024
2048
4096
8192
16384
32768
65536
gap>

Hope this helps
Alexander








More information about the Forum mailing list