Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to print a property of a variable in a table
Started by JoePringles May 18 2019 03:59 PM

- - - - -
5 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 340 posts
  • Corona SDK

Well my title is probably wrong but heres an example of what I want to do:

 
local example_table = {}
local variable


variable.text="blah blah blah"
example_table[1]=variable


print(example_table[1].text)
--This line^ does not work

How can I print variable.text by printing it through the table?

I know I can just do print(variable.text) but lets say I can't do this because of "weird" reasons.

 

thanks a bunch

 

 

PS. dont ask what are these "weird" reasons. because rule #1 in fight club is to never talk about fight club.



[TOPIC: post.html]
#2

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 770 posts
  • Corona SDK

local myTable={}
local variable="blabla"
myTable:insert(variable)
print(myTable.variable)

You cant do variable.text="bla bla" unless the variable is an object. Is that the case here?
  • anaqim likes this

[TOPIC: post.html]
#3

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 770 posts
  • Corona SDK

My code above assumes your variable is an object

[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,585 posts
  • Corona SDK

https://roaminggamer.github.io/RGDocs/pages/SSK2/extensions/#table-debug-features

local rec = {}
rec.name = "bob"
rec.age = 100
rec.inventory = {}

rec.inventory[1] = "coin"
rec.inventory[2] = "game"

-- Dump first level entries in table.
table.dump(rec)

-- recursively print table
table.print_r(rec)


[TOPIC: post.html]
#5

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 340 posts
  • Corona SDK

Thanks for your replies!

Ik I can just probably make that property into a separate object which will save me time in the long run.

But is there no way through corona to print out a property of an object in a table?

@roaminggamer luckily I am using SSK2 so I’ll try that out thanks :)

[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

Yes, you're misunderstanding. 'variable' was never a table, therefore variable.text is not a 'property' of that table. I'm surprised you didn't get a runtime error there.

 

This will work:

 

 
local variable = {}
 
variable.text = "blah blah blah"
 
print (variable.text)
 




[topic_controls]
[/topic_controls]