How to format a lua string with a boolean variable?

106,128

Solution 1

Looking at the code of string.format, I don't see anything that supports boolean values. I guess tostring is the most reasonable option in that case.

Example:

print("this is: " .. tostring(true))  -- Prints:   this is true

Solution 2

In Lua 5.1, string.format("%s", val) requires you to manually wrap val with tostring( ) if val is anything other than a string or number.

In Lua 5.2, however, string.format will itself call the new C function luaL_tolstring, which does the equivalent of calling tostring( ) on val.

Solution 3

You can redefine string.format to support an additional %t specifier that runs tostring on an argument:

do
  local strformat = string.format
  function string.format(format, ...)
    local args = {...}
    local match_no = 1
    for pos, type in string.gmatch(format, "()%%.-(%a)") do
      if type == 't' then
        args[match_no] = tostring(args[match_no])
      end
      match_no = match_no + 1
    end
    return strformat(string.gsub(format, '%%t', '%%s'),
      unpack(args,1,select('#',...)))
  end
end

With this, you can use %t for any non-string type:

print(string.format("bool: %t",true)) -- prints "bool: true"
Share:
106,128

Related videos on Youtube

Michael J. Barber
Author by

Michael J. Barber

Physicist interested in complex systems and machine learning.

Updated on July 09, 2022

Comments

  • Michael J. Barber
    Michael J. Barber 6 months

    I have a boolean variable whose value I'd like to display in a formatted string. I tried using string.format, but get something like the following for any choice of format option listed in the language reference:

    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > print(string.format("%c\n", true))
    stdin:1: bad argument #2 to 'format' (number expected, got boolean)
    stack traceback:
        [C]: in function 'format'
        stdin:1: in main chunk
        [C]: ?
    

    I can get the boolean to display by adding a tostring,

    > print(string.format("%s\n", tostring(true)))
    true
    

    but that seems rather indirect to this lua beginner. Is there an formatting option I've overlooked? Or should I use the above approach? Something else?

    • Jane T
      Jane T over 11 years
      why use string.format? Why not just print(tostring(true))
    • sylvanaar
      sylvanaar over 11 years
      you can also not format nil, function, thread, userdata...
    • Michael J. Barber
      Michael J. Barber over 11 years
      @Jane T Because it's part of a longer string, the example is pared down to the minimum.
    • Jane T
      Jane T over 11 years
      That's fine, then yes you will need to use tostring.
    • Michael J. Barber
      Michael J. Barber over 11 years
      @sylvanaar - That's the sort of information I'm looking for -- can you point me to where that's documented?
    • kikito
      kikito over 11 years
      @Michael You can find that information in the string.format reference: lua.org/manual/5.1/manual.html#pdf-string.format
    • Michael J. Barber
      Michael J. Barber over 11 years
      @kikito - No, that info isn't there. That just has formatting options, while I was hoping to hear what sort of values can't be used with string.format.
    • kikito
      kikito over 11 years
      Emm.. it says "numbers or strings". Everything else is implicitly not accepted.
    • Michael J. Barber
      Michael J. Barber over 11 years
      I'm new at lua---I don't do implicit. ;)