literal strings [[this vs]] "that" and "\" .. more!

Post Reply
Caled
Posts: 399
Joined: Thu Apr 09, 2009 4:45 am

literal strings [[this vs]] "that" and "\" .. more!

Post by Caled » Wed Dec 08, 2010 9:18 am

I have contrived a script which makes it easy to demonstrate a problem, and I think that the solution(s) will answer multiple questions and solve multiple problems that I have with my system at the moment.
Code: [show] | [select all] lua
function helloTimer(a, b)
	local a = (a or "hello")
	local b = (b or "world")

	local func = "doHello(\"" .. a .. "\")" 				-- does work
--	local func = [[doHello(]] .. a .. [[)]] 				-- treats "t" as a string
--	local func = "doHello(\"" .. a .. ",\"" .. b .. "\)" 	-- doesn't call doHello() at all

	echo("\nhelloTimer set with: " .. a .. " and " .. b) 
	tempTimer(1, func)
end

function doHello(a, b)
	if b == nil then
		echo("\nThis is your function saying: " .. a)
	else
		echo("\nThis is your function saying: " .. a .. " " .. b)
	end
end
Note: create an alias that calls: helloTimer()
Run the alias.
You will see:
helloTimer set with: hello and world
This is your function saying: hello
With a 1 second gap between the lines.

Now look at the 3 'func = ' lines. Uncomment the second one.
helloTimer set with: hello and worldLua error:[string "function helloTimer(a, b)..."]:15: attempt
to concatenate local 'a' (a nil value)
SO!
First question is how to use [[literal string quotation with double square brackets]] to duplicate the first line.

Second question is, with either quotes or square brackets, how can I pass the values of both 'a' AND 'b' to the doHello() function, via tempTimer?
(Obviously the second of the two commented-out lines is my attempt at this. Not one I expected to work, but rather one of about a dozen semi-random attempts that I made, each one whackier than the last.)

Iocun
Posts: 174
Joined: Wed Dec 02, 2009 1:45 am

Re: literal strings [[this vs]] "that" and "\" .. more!

Post by Iocun » Wed Dec 08, 2010 10:02 am

First question: Make this:
local func = [[doHello("]] .. a .. [[")]]
You still need the quotation marks in there, so it evaluates to doHello("hello") and not doHello(hello)

Second question: Make this:
local func = "doHello(\"" .. a .. "\",\"" .. b .. "\")"
You were missing two things there. First of all, you need a \" before the comma, so the opening \" before a is closed again; second, you forgot the final quotation mark after the \ after b.
What you wrote would evaluate to: doHello("hello,"world\) which doesn't work of course. The corrected version evaluates as doHello("hello","world").

Caled
Posts: 399
Joined: Thu Apr 09, 2009 4:45 am

Re: literal strings [[this vs]] "that" and "\" .. more!

Post by Caled » Wed Dec 08, 2010 10:20 am

Thank you SO much :D

For the explanations as well, I've been struggling with this for quite a long time now.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest