ITeye博客

Hash自然遍历问题

axgle · 2007-04-11 16:34 · Markdown

h={
    1=>"a",
    21=>"b-1",
    22=>"b-2",
    23=>"b-3",
    3=>"c",
    4=>"d"
}

h.each do |k,v|
   puts k.to_s + "=>" + v
end

结果为:

22=>b-2
23=>b-3
1=>a
3=>c
4=>d
21=>b-1

我期望的结果应为:

1=>a
21=>b-1
22=>b-2
23=>b-3
3=>c
4=>d

一般的问题:Hash里每个元素是按照什么顺序建立的,那么就按照什么顺序遍历.ruby如何处理这个问题?

评论(11)

  1. #11 楼 · axgle · 2007-04-12 17:26

    rails下的view中测试通过:

    <%
    h=ActiveSupport::OrderedHash.new 
    h[1]=3
    h[2]=2
    h[0]=4
    %>    
    
    <% h.each do |k,v|%>
    <%=k.to_s + v.to_s%>
    <%end%>
    
  2. #10 楼 · axgle · 2007-04-12 16:59

    google了一下,rails里有OrderedHash

    require "active_support"
    include ActiveSupport
    
    h= OrderedHash.new  
     
    h[1]="a"
    h[21]="b-1"
    h[22]="b-2"
    h[23]="b-3"
    h[3]="c"
    h[4]="d"
    
    h.each do |k,v|
      puts k.to_s + "=>" + v 
    end
    
  3. #9 楼 · dennis_zane · 2007-04-12 10:42

    weiqingfei writes:

    [quote=dennis_zane][quote=weiqingfei][quote=gigix][quote=weiqingfei]不知道ruby里有没有LinkedHashSet这样的东西、。[/quote] YAGNI[/quote]为什么? ruby用什么来存储有顺序的hash?[/quote]

    You Aren't Gonna Need It[/quote]我问的就是why not, 看楼主的问题是他已经开始需要了。

    自己实现一个也不是很难的事情吧,更简单地上rubyforge查找下就有了,比如这个项目http://rubyforge.org/frs/?group_id=728&release_id=2305

    
    # A QueueHash is an ordered hash: Keys are ordered according to when they were 
    # inserted.
    #
    # The RubyForge project page is at http://rubyforge.org/projects/queuehash .
    
    require 'delegate'
    
    class QueueHash < DelegateClass( Array )
    	Version = '0.1.0'
    	
    	# Creates a QueueHash with all the elements in <tt>array</tt> as keys, and 
    	# each value initially set to be the same as the corresponding key.
    	def self.new_from_array(array)
    		new( *( ( array.map { |elt| [ elt, elt ] } ).flatten ) )
    	end
    
    	# Takes an even number of arguments, and sets each odd-numbered argument to 
    	# correspond to the argument immediately afterward. For example:
    	#   queueHash = QueueHash.new (1, 2, 3, 4)
    	#   queueHash[1] => 2
    	#   queueHash[3] => 4
    	def initialize(*values)
    		@pairs = []
    		0.step(values.size-1, 2) { |i| @pairs << [ values[i], values[i+1] ] }
    		super( @pairs )
    	end
    	
    	def ==( otherObj )
    		if otherObj.class == QueueHash && otherObj.size == size
    			( 0..size ).all? { |i|
    				keys[i] == otherObj.keys[i] && values[i] == otherObj.values[i]
    			}
    		else
    			false
    		end
    	end
    
    	def [](key)
    		( pair = @pairs.find { |pair| pair[0] == key } ) ? pair.last : nil
    	end
    
    	def []=(key, value); @pairs << [key, value]; end
    
    	def each; @pairs.each { |pair| yield pair[0], pair[1] }; end
    
    	def keys; @pairs.map { |pair| pair[0] }; end
    
    	def values; @pairs.map { |pair| pair[1] }; end
    end
    
    
    
    
    
  4. #8 楼 · weiqingfei · 2007-04-12 09:41

    dennis_zane writes:

    [quote=weiqingfei][quote=gigix][quote=weiqingfei]不知道ruby里有没有LinkedHashSet这样的东西、。[/quote] YAGNI[/quote]为什么? ruby用什么来存储有顺序的hash?[/quote]

    You Aren't Gonna Need It 我问的就是why not, 看楼主的问题是他已经开始需要了。

  5. #7 楼 · dennis_zane · 2007-04-12 09:21

    weiqingfei writes:

    [quote=gigix][quote=weiqingfei]不知道ruby里有没有LinkedHashSet这样的东西、。[/quote] YAGNI[/quote]为什么? ruby用什么来存储有顺序的hash?

    You Aren't Gonna Need It

  6. #6 楼 · weiqingfei · 2007-04-12 09:16

    gigix writes:

    [quote=weiqingfei]不知道ruby里有没有LinkedHashSet这样的东西、。[/quote] YAGNI 为什么? ruby用什么来存储有顺序的hash?

  7. #5 楼 · gigix · 2007-04-12 09:03

    weiqingfei writes:

    不知道ruby里有没有LinkedHashSet这样的东西、。

    YAGNI

  8. #4 楼 · weiqingfei · 2007-04-11 23:05

    不知道ruby里有没有LinkedHashSet这样的东西、。

  9. #3 楼 · 天之魔神 · 2007-04-11 19:18

    哈希表应该没有顺序

  10. #2 楼 · dennis_zane · 2007-04-11 17:39

    Hash,联想到其他语言的类似Hash之类的数据结构(比如java的HashTable,HashMap),都是无序的,应该是根据哈希码来查找的吧。

  11. #1 楼 · gigix · 2007-04-11 16:40

    hash 这个词本身就包含了“无序”的意思

评论功能维护中,当前仅展示历史评论。