Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by CaKeMeaT · Jun 14, 2015 at 07:03 PM · dictionary

get value of Dictionary keys via index number?

I am wondering if I can get or extract the string name of the Key in a Dictionary

I have seen examples accessing the key from inside a foreach, but I need a single index approach. The index will be decided by random int.

     public void rng_head()
     {
 
         int rnd_q = Random.Range (1, 5);
         int rnd_t = Random.Range (1, 5);
 
         // THIS IS HOW I WOULD GET FROM ARRAY or 2D ARRAY
         //string output = head_quality [rnd_q].ToString () + " " + head_type [rnd_t].ToString ();
         //string output_id = head_quality [rnd_q].ToString () + " " + head_type [rnd_t].ToString ();
 
         // BUT I WANT TO GET FROM DICTIONARY??
         string output = dict_head_quality [rnd_q];
         string output_id = dict_head_type [rnd_t];
 
         // dictionary should keep perfectly accurate display name vs id_name
         // so when items are generated they have a nice display name, but also 
         // an exact, lowercase, id name
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by fafase · Jun 14, 2015 at 07:50 PM

A dictionary does not use index since it is not its initial purpose. A dictionary uses key-value relation. It does not so because items are not stored continuously in memory. You have one reference which knows where all items are. But none is first or last. Like when you look in the fridge, you know the eggs are on the door top shelves and the vegetables are on the bottom drawer but which is first then?

You could build one with int,object relation where the int is the key and works as an index. It would be like a list with non continuous indexes.

Your case looks like you should think of list/array instead.

If you really need to store in a dictionary and want to have something random out of it, maybe something like this:

 string GetRandomValue(Dictionary<string,string> dict) // Dunno what is your dictionary types...
 {
     int rnd = Random.Range (1, 5);
     int index = 0;
     foreach(var kvp in dict){
         index++;
         if(index == rnd){
            return kvp.Key;  // According to question, you are after the key 
         } 
     }
     return null;
 }





Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Erfo77 · Aug 08, 2021 at 06:46 AM

You can also index the dictionary values by yourself enveloping the key-value pair you are interested into a KeyValuePair and associating it with an int like so:

 Dictionary<int, KeyValuePair<string, int>> indexedDictionary = new Dictionary<int, KeyValuePair<string, int>>
     {
         {0, new KeyValuePair<string, int>("my entry", 13) },
         {1, new KeyValuePair<string, int>("whatever", 5) },
         {............}
     };


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 08, 2021 at 09:30 AM 0
Share

In this case it's pointless to use a Dictionary. A list of KeyValuePairs would make much more sense. The point of a dictionary is to provide fast lookup of keys that are not indices. packing KeyValuePairs like this into a List or dictionary means you loose the ability to lookup keys fast.


If you need both, a dictionary lookup of keys and you want to randomly select keys through an index, you can create a seperate string list / array that contains all keys. So you can pick a random key from that array / list and use that key in the dictionary.

avatar image Erfo77 Bunny83 · Aug 08, 2021 at 10:21 AM 0
Share

Indexing a dictionary is not what a dictionary was intended for. That's for sure. That said, if you do need to access its values and keys also via an index reference, depending on the use case a List may not be appropriate as in a List the indexes will change as you remove or add to it

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dictionary vs Hashtable 1 Answer

Serializing data structures the Unity way. Possible? 1 Answer

Determine an index of a closest number in a database, from a user's input 1 Answer

CustomEditor for 2 List to look like dictionary 1 Answer

How to serialize a Dictionary? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges