[LUA] string.format arguments

User avatar
Gazprom
Scavenger
Posts: 13
Joined: 25 Jul 2017, 03:46

[LUA] string.format arguments

Postby Gazprom » 08 Aug 2017, 06:20

Hello everyone,
I'm having an hard time parsing a string through string.format.
my string is like:
str = ' "text1","text2","1","2","3" ' (it's a table.concat() result)
and for assigning it to their keys:
local string1, string2, string3, string4, string5 = string.format ( "%s, %s, %c, %c, %c", str)
but it keeps reporting me:
bad argument #3 to 'format' (no value)

Do I have to pass them as %q instead? I have no clues about unfortunately.

AxelDominatoR
Trespasser
Posts: 54
Joined: 19 Aug 2014, 20:14
Location: Glasgow
Contact:

Re: [LUA] string.format arguments

Postby AxelDominatoR » 08 Aug 2017, 10:48

If I remember correctly string.format is used to put multiple separate variables into a single string. You are trying to do the opposite (split a string into multiple variables) so something like string.match may be more appropriate.
Here's an example you can adapt: https://stackoverflow.com/questions/201 ... -variables
Axel DominatoR

User avatar
Alundaio
S.T.A.L.K.E.R.
Posts: 1367
Joined: 26 May 2012, 22:26

Re: [LUA] string.format arguments

Postby Alundaio » 08 Aug 2017, 23:28

string.format only returns the formated string you give it as param 1.

printf( string.format ( "%s, %s, %d, %d, %d", str) )

%c is for char byte. You want to continue to use %s if it's just "1" or %d if it's a number 1.

Code: Select all

string.format(s, e1, e2, ...)

s:format(e1, e2, ...)

Create a formatted string from the format and arguments provided. This is similar to the printf("format",...) function in C. An additional option %q puts quotes around a string argument's value.

c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument.
q and s expect a string.
> = string.format("%s %q", "Hello", "Lua user!")   -- string and quoted string
Hello "Lua user!"
> = string.format("%c%c%c", 76,117,97)             -- char
Lua
> = string.format("%e, %E", math.pi,math.pi)       -- exponent
3.141593e+000, 3.141593E+000
> = string.format("%f, %g", math.pi,math.pi)       -- float and compact float
3.141593, 3.14159
> = string.format("%d, %i, %u", -100,-100,-100)    -- signed, signed, unsigned integer
-100, -100, 4294967196
> = string.format("%o, %x, %X", -100,-100,-100)    -- octal, hex, hex
37777777634, ffffff9c, FFFFFF9C
"I have a dream that one day this community will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident; that all mods are created equal."

User avatar
Gazprom
Scavenger
Posts: 13
Joined: 25 Jul 2017, 03:46

Re: [LUA] string.format arguments

Postby Gazprom » 09 Aug 2017, 03:59

Thanks everyone. In the end I ended up trying every method I could (while reading the Lua manual chapter for some tips :P ), and I found the one suggested by Axel to be working:

local mytext, num = string.gsub(table.concat(mytable), '"', '') --I prepared the string for the parsing
local param1, param2, param3, param4, param5= mymessage:match("([^,]+),([^,]+),([^,]+),([^,]+),(.+)") --used string.match method


@Alundaio: I kept on trying that, but I think that in the end it was my string to be faulty, that's because I passed it on a gsub before splitting it.

As a total beginner it was a pain writing down the regular expression part, so if everyone needs it here it is.

Many thanks to everyone again!

User avatar
Alundaio
S.T.A.L.K.E.R.
Posts: 1367
Joined: 26 May 2012, 22:26

Re: [LUA] string.format arguments

Postby Alundaio » 09 Aug 2017, 17:51

If it's in a table and you are concating it to a string, then why are you splitting it back into parts again? If you only want each part, you already have them. Just access each one with mytable[1], mytable[2], mytable[3], etc.
"I have a dream that one day this community will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident; that all mods are created equal."

User avatar
Gazprom
Scavenger
Posts: 13
Joined: 25 Jul 2017, 03:46

Re: [LUA] OT

Postby Gazprom » 10 Aug 2017, 03:01

The table was the one filled by ltn12.sink.table(), and I arranged the response text to be equal as a table indexed content, i.e. "post1","post2","post3" for that exact reason, but retrieving the values through the indexes made the game crash. My bet was that even if written like a table content was still conceived as one single string, and as such only one index.

Big OT: I'm trying to make a loop with delay function, I took inspiration from here:
http://www.gsc-game.com/index.php?t=community&s=forums&s_game_type=xr3&thm_page=65&thm_id=5790&sec_id=18&page=1
but nothing seems to work. I've even written a wait function taking inspiration form here:
https://stackoverflow.com/questions/17987618/how-to-add-a-sleep-or-wait-to-my-lua-script

Code: Select all

function wait(n)
local wait_time = time_global() + (n*1000) --n was in seconds
local tg = time_global()
while tg < wait time do
end
end

and made the interior of the function look like:

Code: Select all

function test()
if db.actor and level.present() then
...
while condition == "true" do
ActionStations(2,6)
wait(2)
end
end
end

But the game won't even load, it stucks itself to the loading screen but without the "map" loading screenshot, and interesting fact, it doesn't crash, it's just stuck there.
Readapting your loop from the GSC site seems not to affect the game at all unfortunately, is there any other way I can do that?

Also, how does the leaderboard system work? I've noticed that there's a engine binded function in one of the PDA scripts, but still haven't figured out how does it works.
SOrry for the really, really big OT :oops:

sLoPpYdOtBiGhOlE
Scavenger
Posts: 18
Joined: 12 Jun 2017, 23:44

Re: [LUA] string.format arguments

Postby sLoPpYdOtBiGhOlE » 10 Aug 2017, 20:39

For a time delayed function call, maybe try the CreateTimeEvent() function found in _g.script

Works a treat for calling functions on a timed delay and has a very short learning curve to use and is pretty flexible.
(Well I'm a below novice lua scripter and it took a whole of a couple of minutes to get a grasp of how to use it)

User avatar
Gazprom
Scavenger
Posts: 13
Joined: 25 Jul 2017, 03:46

Re: [LUA] string.format arguments

Postby Gazprom » 12 Aug 2017, 16:01

Thanks, will do!
One thing, it says that for a continuous loop the function must return false. It means the function where CreateTimeEvent is invoked, right?

sLoPpYdOtBiGhOlE
Scavenger
Posts: 18
Joined: 12 Jun 2017, 23:44

Re: [LUA] string.format arguments

Postby sLoPpYdOtBiGhOlE » 12 Aug 2017, 21:56

Not the function that invokes CreateTimeEvent, but the function that CreateTimeEvent calls.

CreateTimeEvent calls your function repeatedly while it returns false.
Your function stays in the CreateTimeEvent queue until your called function returns true.

Keep in mind that CreateTimeEvent queued events are not saved to file on game save or quit.
So you need to re add your events when your game loads or where ever your event needs to be implemented.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests