Variables

I was looking into variables, and I found the following code:

print( nameOfVariable )
nameOfVariable = “VariableValue”
print( nameOfVariable )

What does print mean, and why is it at both the beginning and end of the code?
Thanks,
Bittersweet [import]uid: 27919 topic_id: 5793 reply_id: 305793[/import]

The print function outputs something to the Corona Terminal/Console, and in this case I assume it is printing nameOfVariable before to show you that there isn’t anything in it yet. Then it sets a value to it, and prints it again, showing you that nameOfVariable now has a value. [import]uid: 8782 topic_id: 5793 reply_id: 19881[/import]

So are you saying that it will show the value of the variable on the screen? [import]uid: 27919 topic_id: 5793 reply_id: 20132[/import]

print(“hello world”) will print hello world on the terminal.
Look at the “hello world” tutorial which explains terminal output and screen output. You are trying to learn a language, lua/corona; the api reference examples, tutorials and sample code will be a HUGE help to you. Pick something that looks interesting and take it apart until you understand it. Please… [import]uid: 12635 topic_id: 5793 reply_id: 20193[/import]

If you’d like to see the variable printed in your app, try something like this:

[lua]local myFont = native.systemFontBold
local alertCount = 1
local alertArray = {}

function alert(theText, append)

– If “append” is set to “false”
if not append then

– Remove any previous text blocks
for i,item in ipairs( alertArray ) do
item:removeSelf()
end

– Reset the counter
alertCount = 1
end

– Create a new “object” on the alertArray that contains a “quassi-variable” called “handle”
alertArray[alertCount] = { handle = “” }

– Set the “handle” variable within our newly created “object” to a newly created text block
alertArray[alertCount].handle = display.newText(theText, 20, alertCount*20, myFont, 20 )

– Set the color of the text block
alertArray[alertCount].handle:setTextColor(255,0,255)

– Increase our counter by one so that we can have more lines, should we choose to set “append” to true
alertCount = alertCount + 1
end

–[[
Usage Example:

function doSomeMath()
local problem = 2 + 2
alert(“yo”, false)
end

function doMoreMath()
local problem = 4 + 4
alert(problem, true)
end

doSomeMath()
– shows: 4

doSomeMath()
doSomeMath()
doSomeMath()

– doMoreMath uses the “append”:

doMoreMath()
– shows: 4
– 8

doMoreMath()
doMoreMath()
doMoreMath()
– shows: 8
– 8
– 8
– 8
–]][/lua] [import]uid: 616 topic_id: 5793 reply_id: 22193[/import]