Karim Belabas on Wed, 15 Aug 2018 17:36:16 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: printf without spaces


* John Cremona [2018-08-15 16:51]:
> I would like formatted printing of a list (of positive integers) with no
> embedded spaces.  The %s conversion inserts spaces:
> 
> ? v = vector(10,j,j)
> %80 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> ? printf("%s", v)
> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> 
> (there's a space after each comma).  Is that possible with some more %
> magic?  I could not see how to.

Not really. You can 

1) (globally) set 'output' to 0 [ changing globals is not a good
programming practice ]

2) print each element individually [ cumbersome but can be done once and for all]

  vtostr(v) = 
  { my (w, n = #v);
    if (!n, return (""));
    w = vector(n);
    for (i = 1, n-1, w[i] = Str(v[i],","));
    w[n] = Str(v[n]);
    concat(w);
  }
  ? printf("[%s]", vtostr(v))
  [1,2,3,4,5,6,7,8,9,10]

Maybe someone else will have a better idea :-)

> Second question: in the first line above is there an easier way to get a
> range of consecutive integers into a list?

  v = [1..10];

Cheers,

    K.B.
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux         Fax: (+33) (0)5 40 00 21 23
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~kbelabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]
`