# On Hashes - How the hashes printing works?

**URL:** <https://rubytalk.org/t/on-hashes-how-the-hashes-printing-works/53596>\
**Category:** ruby-talk\
**Created:** [27 May 2009 08:16 UTC](https://rubytalk.org/t/on-hashes-how-the-hashes-printing-works/53596 "2009-05-27T08:16:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_Ch](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Neela\_megha\_shyam\_Ch](https://rubytalk.org/u/Neela_megha_shyam_Ch)\
**Post date:** [27 May 2009 08:16 UTC](https://rubytalk.org/t/on-hashes-how-the-hashes-printing-works/53596/1 "2009-05-27T08:16:57Z")

</div>

Hi All,

I have my code as follows:-

people = {  
"torvalds"=\>{"lname"=\>"Torvalds", "fname"=\>"Linus",  
"job"=\>"maintainer"}, "matsumoto"=\>{"lname"=\>"Matsumoto",  
"fname"=\>"Yukihiro", "job"=\>"Ruby originator"},  
"litt"=\>{"lname"=\>"Litt", "fname"=\>"Steve", "job"=\>"troubleshooter"}  
}  
keys = people.keys  
for key in 0...keys.length  
print "key : ", keys[key], "\n"  
print "lname: ", people[keys[key]]["lname"], "\n"  
print "fname: ", people[keys[key]]["fname"], "\n"  
print "job : ", people[keys[key]]["job"], "\n"  
print "\n\n"  
end

O/p:-  
key : litt  
lname: Litt  
fname: Steve  
job : troubleshooter  
key : matsumoto  
lname: Matsumoto  
fname: Yukihiro  
job : Ruby originator  
key : torvalds  
lname: Torvalds  
fname: Linus  
job : maintainer

Question:-  
Why key of "litt" is printed first thought the first key of hash is  
"torvalds"?

Thanks and regards,  
Neela.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Markus\_Schirp](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@Markus\_Schirp](https://rubytalk.org/u/Markus_Schirp)\
**Post date:** [27 May 2009 08:25 UTC](https://rubytalk.org/t/on-hashes-how-the-hashes-printing-works/53596/2 "2009-05-27T08:25:53Z")

</div>

Hashes enumeration does not mirror insertion order.  
Hashes are not ordered:

> **[Hash table](https://en.wikipedia.org/wiki/Hash_table#Drawbacks)**
>
> In computing, a hash table, also known as a hash map or a hash set, is a data structure that implements an associative array, also called a dictionary, which is an abstract data type that maps keys to values. A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found. During lookup, the key is hashed and the resulting hash indicates where the corresponding value is stored.
> Ideally, the hash functi...

Markus

> **···**
>
> On Wed, May 27, 2009 at 05:16:57PM +0900, Neela megha shyam Chivukula wrote:
> 
> > Hi All,
> > 
> > I have my code as follows:-
> > 
> > people = {  
> > "torvalds"=\>{"lname"=\>"Torvalds", "fname"=\>"Linus",  
> > "job"=\>"maintainer"}, "matsumoto"=\>{"lname"=\>"Matsumoto",  
> > "fname"=\>"Yukihiro", "job"=\>"Ruby originator"},  
> > "litt"=\>{"lname"=\>"Litt", "fname"=\>"Steve", "job"=\>"troubleshooter"}  
> > }  
> > keys = people.keys  
> > for key in 0...keys.length  
> > print "key : ", keys[key], "\n"  
> > print "lname: ", people[keys[key]]["lname"], "\n"  
> > print "fname: ", people[keys[key]]["fname"], "\n"  
> > print "job : ", people[keys[key]]["job"], "\n"  
> > print "\n\n"  
> > end
> > 
> > O/p:-  
> > key : litt  
> > lname: Litt  
> > fname: Steve  
> > job : troubleshooter  
> > key : matsumoto  
> > lname: Matsumoto  
> > fname: Yukihiro  
> > job : Ruby originator  
> > key : torvalds  
> > lname: Torvalds  
> > fname: Linus  
> > job : maintainer
> > 
> > Question:-  
> > Why key of "litt" is printed first thought the first key of hash is  
> > "torvalds"?
> > 
> > Thanks and regards,  
> > Neela.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_Ch](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Neela\_megha\_shyam\_Ch](https://rubytalk.org/u/Neela_megha_shyam_Ch)\
**Post date:** [27 May 2009 08:28 UTC](https://rubytalk.org/t/on-hashes-how-the-hashes-printing-works/53596/3 "2009-05-27T08:28:21Z")

</div>

Markus Schirp wrote:

> Hashes enumeration does not mirror insertion order.  
> Hashes are not ordered:
> 
> [Hash table - Wikipedia](http://en.wikipedia.org/wiki/Hash_table#Drawbacks)
> 
> Markus

Thank you Markus.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jarmo\_Pertman](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@Jarmo\_Pertman](https://rubytalk.org/u/Jarmo_Pertman)\
**Post date:** [28 May 2009 06:30 UTC](https://rubytalk.org/t/on-hashes-how-the-hashes-printing-works/53596/4 "2009-05-28T06:30:42Z")

</div>

Jou could use orderedhash gem to have hashes ordered like arrays for  
example if you really need it for some reason:  
[http://codeforpeople.com/lib/ruby/orderedhash/](http://codeforpeople.com/lib/ruby/orderedhash/)

Markus Schirp wrote:

> **···**
>
> > Hashes enumeration does not mirror insertion order.  
> > Hashes are not ordered:
> > 
> > [Hash table - Wikipedia](http://en.wikipedia.org/wiki/Hash_table#Drawbacks)
> > 
> > Markus
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Markus\_Schirp](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@Markus\_Schirp](https://rubytalk.org/u/Markus_Schirp)\
**Post date:** [28 May 2009 10:56 UTC](https://rubytalk.org/t/on-hashes-how-the-hashes-printing-works/53596/5 "2009-05-28T10:56:34Z")

</div>

Or maintain an "order of insertion key array".

> **···**
>
> On Thu, May 28, 2009 at 03:30:42PM +0900, Jarmo Pertman wrote:
> 
> > Jou could use orderedhash gem to have hashes ordered like arrays for  
> > example if you really need it for some reason:  
> > [http://codeforpeople.com/lib/ruby/orderedhash/](http://codeforpeople.com/lib/ruby/orderedhash/)
> > 
> > Markus Schirp wrote:  
> > \> Hashes enumeration does not mirror insertion order.  
> > \> Hashes are not ordered:  
> > \>  
> > \> [Hash table - Wikipedia](http://en.wikipedia.org/wiki/Hash_table#Drawbacks)  
> > \>  
> > \> Markus
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
