Printf / indent

How can I control starting (left) indentation for printf? Pickaxe did not
yield an answer. Or do I have to loop putting out spaces with
IO#putc " "?

Thanks!

Shouldn’t sprintf truncate strings if needed?

sprintf("%10s", “Hello”) --> " Hello": correct padding
sprintf("%3s", “Hello”) --> “Hello”: no truncation?

Bug? Operator error?

“Its Me” itsme213@hotmail.com wrote in message
news:cuyhc.9568$hR1.4441@fe2.texas.rr.com

···

How can I control starting (left) indentation for printf? Pickaxe did not
yield an answer. Or do I have to loop putting out spaces with
IO#putc " "?

Thanks!

Its Me wrote:

How can I control starting (left) indentation for printf? Pickaxe did not
yield an answer. Or do I have to loop putting out spaces with
IO#putc " "?

Thanks!

I would do

indent = 3
message = "Hello, world"
printf “%#{indent}s%s\n”, “”, message

or

indent = 3
message = "Hello, world"
printf “#{’ '*indent}%s”, message

Gennady.

“Its Me” itsme213@hotmail.com writes:

Shouldn’t sprintf truncate strings if needed?

No. From the UNIX manual page printf(3):

   In no case does a non-existent or small field width cause
   truncation of a field; if the result of a conversion is
   wider than the  field  width, the field is expanded to
   contain the conversion result.

-Mark

Its Me wrote:

Shouldn’t sprintf truncate strings if needed?

sprintf("%10s", “Hello”) --> " Hello": correct padding
sprintf("%3s", “Hello”) --> “Hello”: no truncation?

If you need truncation you should use
sprintf("%3.3s,“Hello”)

The precision field for strings in C printf (and in Ruby’s consequently)
is just that – specifies the maximum size of a filed.

Gennady.

···

Bug? Operator error?

“Its Me” itsme213@hotmail.com wrote in message
news:cuyhc.9568$hR1.4441@fe2.texas.rr.com

How can I control starting (left) indentation for printf? Pickaxe did not
yield an answer. Or do I have to loop putting out spaces with
IO#putc " "?

Thanks!

Thank you, thank you, thank you!

I don’t know how long it would have taken me to figure that out …

“Gennady” gfb@tonesoft.com wrote in message
news:4086BE43.1000606@tonesoft.com

Its Me wrote:

How can I control starting (left) indentation for printf? Pickaxe did
not

···

yield an answer. Or do I have to loop putting out spaces with
IO#putc " "?

Thanks!

I would do

indent = 3
message = "Hello, world"
printf “%#{indent}s%s\n”, “”, message

or

indent = 3
message = "Hello, world"
printf “#{’ '*indent}%s”, message

Gennady.

“Gennady” gfb@tonesoft.com schrieb im Newsbeitrag
news:4086BE43.1000606@tonesoft.com

Its Me wrote:

How can I control starting (left) indentation for printf? Pickaxe did
not

yield an answer. Or do I have to loop putting out spaces with
IO#putc " "?

Thanks!

I would do

indent = 3
message = "Hello, world"
printf “%#{indent}s%s\n”, “”, message

or

indent = 3
message = "Hello, world"
printf “#{’ '*indent}%s”, message

printf style is rather this:

irb(main):007:0> printf “%*d”, 10, 5
5=> nil
irb(main):008:0> printf “%*d”, -10, 5
5 => nil

robert

Gennady wrote:

Its Me wrote:

Shouldn’t sprintf truncate strings if needed?

sprintf("%10s", “Hello”) --> " Hello": correct padding
sprintf("%3s", “Hello”) --> “Hello”: no truncation?

If you need truncation you should use
sprintf("%3.3s,“Hello”)

Correction:
sprintf("%3.3s",“Hello”)

(missed the quote)

···

The precision field for strings in C printf (and in Ruby’s consequently)
is just that – specifies the maximum size of a filed.

Gennady.

Bug? Operator error?

“Its Me” itsme213@hotmail.com wrote in message
news:cuyhc.9568$hR1.4441@fe2.texas.rr.com

How can I control starting (left) indentation for printf? Pickaxe did
not
yield an answer. Or do I have to loop putting out spaces with
IO#putc " "?

Thanks!

Robert Klemme wrote:

printf style is rather this:

irb(main):007:0> printf “%*d”, 10, 5
5=> nil
irb(main):008:0> printf “%*d”, -10, 5
5 => nil

robert

Hmm, thanks. I forgot about this possibility.
Gennady.