Jump to content


Photo

Return first key of the item of table?


  • Please log in to reply
5 replies to this topic

#1 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 06 October 2018 - 10:44

The table:

table = { ["name"]="formula1",  ["man"] ="formula2" }

How print or return "name" or "man"?

 

Note: print( table["name"]) --> "formula1". How to do the opposite without the need to create a table with the inverted equality? Is there any way?


Edited by Raul (ulneiz), 06 October 2018 - 11:12.

:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#2 joule

joule

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1863 posts
  • Gender:Not Telling

Posted 06 October 2018 - 10:49

Semantically, you've stated three different questions. Please clarify. What result do you want?



#3 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 06 October 2018 - 10:52

Semantically, you've stated three different questions. Please clarify. What result do you want?

 

The table:

table = { ["name"]="formula1",  ["man"] ="formula2" }

How print "name"?


:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#4 danoise

danoise

    Probably More God or Borg Than Human Member

  • Renoise Team
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 7008 posts
  • Gender:Male
  • Location:Berlin
  • Interests:wildlife + urban trekking

Posted 06 October 2018 - 10:58

In lua, only tables that use integers as keys have a specific order. 

So in you case, there is no guarantee that "name" is the first entry, even if you created it the way you posted. 

 

Only if you settle for e.g. extracting the keys of the table through table.keys(your_table) and then doing a table.sort() on the result will you get a fixed order. 


  • Raul (ulneiz) likes this

Tracking with Stuff. API wishlist | Soundcloud


#5 joule

joule

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1863 posts
  • Gender:Not Telling

Posted 06 October 2018 - 11:04

The table:

table = { ["name"]="formula1",  ["man"] ="formula2" }

How print "name"?

 

OK. I think I'm smart enough to understand that you don't want me to answer 'print("name")'

tbl = { ["name"]="formula1",  ["man"] ="formula2" }
 
print(table.find(tbl, "formula1"))

Do note that if "formula1" is the value of more than one key, there is no guarantee of which key that will be returned.


Edited by joule, 06 October 2018 - 11:04.

  • Raul (ulneiz) likes this

#6 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 06 October 2018 - 11:11

OK. I think I'm smart enough to understand that you don't want me to answer 'print("name")'

 

^_^  ^_^  ^_^ I took it for granted!

 

tbl = { ["name"]="formula1",  ["man"] ="formula2" }
 
print(table.find(tbl, "formula1"))

Do note that if "formula1" is the value of more than one key, there is no guarantee of which key that will be returned.

 

Ok perfect, this will serve me. Yes, the equalities are all different. Thank you very much!  :)


  • joule likes this

:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler