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
2
Question by GlennHeckman · Oct 12, 2010 at 06:47 PM · treeienumeratorhashtable

Enumerator, why don't you love me?

Ok,

I'm attempting to duplicate an XML document within Unity as a series of nested Hashtables. This works fine (so far) .. my problem is this:

In a Javascript Script, I can access the hashtable's properties as such:

myHashtable["atts"]["expand"] (works perfectly, however,)

I'm trying to access the same location within the hashtable, but through a C# script. I'm getting .. Index not allowed or something along those lines. I've read that to loop through a hashtable's properties, I should use Enumerator = GetEnumerator() .... I'm unfamiliar with C# (Flash AS background).

In short .. I want to loop through and see the properties/values from a hashtable in a C# Script and then use those values as titles for GUI buttons (think Expandable Tree Menu)

Can anybody help me out?! If I'm being unclear with my question, please let me know and I'll try to clarify.

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by duck · Oct 12, 2010 at 06:55 PM

To loop through every item in a Hashtable in C#:

    foreach (DictionaryEntry entry in yourHashtable)
    {
        Debug.Log("Key: "+entry.Key+" Value: "+entry.Value);
    }

As for recursively parsing a series of nested Hashtable, it is basically a case of making a function which accepts a hashtable as input, and inside that function, it loops through checking whether the current "entry.Value" is itself a Hashtable. If so, it calls "itself", passing the newly found Hashtable as the input:

void Start() { string result = Parse(nestedHashtables); }

string Parse( Hashtable data ) { foreach(DictionaryEntry entry in data) { if (entry.Value is Hashtable) { Parse(entry.Value); } else { // it's some kind of meaningful value!
} } }

You would then probably want to add some extra arguments to the recursive function so that data can be appended to a "flattened" version of the array, or keep track of the relative "depth" of the current recursion, etc.

Comment
Add comment · Show 5 · 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 Bampf · Oct 12, 2010 at 07:06 PM 0
Share

Yeah, what he said. Alternatively, you can use the Hashtable method $$anonymous$$eys():
foreach (string key in myHashTable.$$anonymous$$eys) { string val = HashTable[key] as string; / do something with val here / } Note that the keys and the values of Hashtable aren't limited merely to strings.

avatar image duck ♦♦ · Oct 12, 2010 at 07:23 PM 0
Share

It's also important to note that when you retrieve a $$anonymous$$ey or Value from a Hashtable, you typically have to re-cast it to the correct type, because it comes out as an "Object" type.

avatar image duck ♦♦ · Oct 12, 2010 at 07:33 PM 0
Share

Oh, and another thing, if you have nested hashtables, you'll probably be wanting to look into recursive parsing! Fun! :-)

avatar image GlennHeckman · Oct 12, 2010 at 07:35 PM 0
Share

Boy did you hit the recursive parsing nail on the head! .. I'm actually working on that portion at the moment. $$anonymous$$y hashtable nesting goes pretty deep (depending on the imported X$$anonymous$$L structure). Any ideas?

avatar image duck ♦♦ · Oct 12, 2010 at 08:21 PM 0
Share

added example of recursive parsing of Hashtable

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Adding trees dynamically - part 2 (the mission) 1 Answer

Tree placing script 0 Answers

"Mass place trees" vs -place trees with brush- vs -my own script- 0 Answers

Can Terrain Placed objects play certain sounds? 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