Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> Hash order
ze1
post Jun 23 2011, 04:45 PM
Post #1


Level 2
Group Icon

Group: Member
Posts: 20
Type: None
RM Skill: Undisclosed




Is it possible somehow to get a hash in the order it was created? For example:

CODE
myhash = {
  "one" => 1,
  "two" => 2,
  "three" => 3,
}
myhash.each { |item}
  p item
}


I want myhash to be printed in the order it was created, but I can't do that. If I can't do that with a hash, can I do it using another structure? I don't think I can use a string as an array index.
Go to the top of the page
 
+Quote Post
   
Callista
post Jun 23 2011, 05:36 PM
Post #2



Group Icon

Group: Revolutionary
Posts: 130
Type: None
RM Skill: Undisclosed




You can't do it in Ruby 1.8 without some workarounds like using string literals containing numbers ("2") as keys instead of the actual word and sorting the hash, but then you'll just be sorting an array of arrays. Why do you need to use a string with literal numbers as an index?


__________________________

"The tragedy of life is what dies inside a man while he lives."

~~~
Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 23rd May 2013 - 01:24 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker