Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] About the self-rearranging of a hash's contents...

So yeah, I have this nice little hash:
Code:
testhash = {'value_01' => 1,
            'value_AX' => 23,
            'value_06' => 5,
            'seperator1' => '-----',
            'value_04' => 112}
So yeah, when I print it directly after the definition, it's already rearranged to this:
{"value_01" => 1, "value_AX" => 23, "value_04" => 112, "seperator1" => "-----", "value_06" => 5}
Well, I would try to understand what's going on, but I have no idea based on what facts they'd rearrange themselves... until this one, I was sure that string-containing keys were ordered after integer-containing keys, but I guess this hash proves me wrong even with that...
So yeah, you'd greatly help me by telling me how I keep the hashes order I assign (though I figure that's not really possible since they don't have index numbers, huh...) or how the self-arranging thing works... like some kind of algorhytm.

My suspection as of now is that I need to convert it to an Array to use the sort method... or is there another method?
 
Actually, that's the beauty of the hashes : they don't have any order. Of course, "print" have to choose an order (Hash#each, probably), but they shouldn't need one. That's what arrays are for, duh.

If you really need indexes, you could create your own class inheriting form Hash...

The relevant functions are each, each_pair, each_key, each_value.

Anyway, you should never need such a hack : hashes don't have to be ordered, or you don't really need hashes.
 
I have a tendancy for disliking hashes' random nature, though they ARE a handy way to store data in some circumstances. I like being able to refer to a set of data based on an ordered number though :P
 

Zeriab

Sponsor

Ruby reference":3be6s4fc said:
A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. The order in which you traverse a hash by either key or value may seem arbitrary, and will generally not be in the insertion order.
The idea behind a hash is to provide a mapping from one object to another. (You can retrieve the value object if you have the key object)
The underlying structure is unknown. My guess would be red-black trees or hash tables, but I don't know. In addition some kind of hash function used on the key object to give a number which is used for storing and retrieving the value objects to and from the underlying data structure. (The .hash method is used on the key object)
You can have more keys pointing to the save value, but not more values pointing to the same keys.

To reiterate what rataime said. You shouldn't use a hash if the order of the collection is needed. (I want to stress the fact ^^)

Could you instead tell us the context? What are you trying to do?
It could be that using a different approach is better.

*hugs*
- Zeriab
 
Well, my intention is to fill a command window with the hash's contents, to be more descriptive: The keys.
Code:
@command_window = Window_CustomCommand.new(544, Module.testhash.keys)
What you do with the command window is changing the assigned values, so you can't really easily make it an array... you could of course make it two arrays, but I'd dislike having to ask for the indexes, so yeah... The best way I can think of with my very little knowledge of hashes is using the hash.sort method, which converts the hash to an array, as far as I know... anyone having a better idea? (like a container that has like an index AND and key to reference to, as well as a value, of course... I might just be dreaming...)

EDIT: Now I forgot the "thanks in advance" twice, which is of course very provey on that you can believe me... =_= However, I'd indeed very apprechiate it, and I apprechiate that semi-legends keep striving through my topics and try to help, so yeah... keep that up, please? ^^"
 
Have you considered creating an array filled with arrays? It's another simply way to store data together, but in an ordered manner.
so you could go
array = [["value1,50]",["value2,23]...etc]
to check what value a certain "key" has you could go
Code:
for i in 0...array.size
  array[i].include?("value1")
  p array[i][1]
end
bit of a weird, roundabout way of doing stuff, but it works...in a way :P
 
I have, but without thinking about the include method for whatever reason... it's what I use all the time normally >_< So yeah, thanks for pointing that out, I'll consider using that, unless it clashes with the code I have until now...

@All: Consider this thread resolved unless you have a mindblowing way to solve this.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top