John Cremona on Wed, 15 Aug 2018 17:45:10 +0200


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

Re: printf without spaces


Thanks, Karim.

On 15 August 2018 at 16:36, Karim Belabas <Karim.Belabas@math.u-bordeaux.fr> wrote:
* 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 :-)

I'll use that.  By the way in my original message I mentioned fprintf since I had seen it in the manual but in fact it is given as an example:

fprintf(file,format,args[..]) = write(file,call(Strprintf,[format,args]));

So I put that into my script and it works like a charm.
 

> 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];

You'll have to believe me that I looked for this....

Thanks again,

John
 

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]
`