From: James Edward Gray II Date: 2006-08-14T00:27:07+09:00 Subject: Re: A use case for an ordered hash On Aug 13, 2006, at 3:25 AM, Robert Klemme wrote: > Hal Fulton wrote: >> There have been numerous occasions when I wanted an >> ordered hash, but usually I can't remember to write >> them down. >> Here's just one. >> Once I wanted a "dynamic case statement" of sorts. >> I wanted to use procs as values in a hash. Something >> like: >> actions = { /abcd/ => lambda { do_this }, >> /xyz/ => lambda { do_that }, >> /abc/ => lambda { other }} >> Then I could just iterate through the keys looking >> for a match, then use that key to find the associated >> proc and call it. >> However, I quickly noticed that this is no good. The >> order of iteration is unpredictable. So I couldn't >> guarantee that (for example) /abcd/ would be tested >> before /abc/, and so on. >> So yeah, I ended up using an array of arrays. But it >> just felt wrong. > > To me a Hash feels wrong. Why? Because you don't make any use of > hash properties for fast lookup. You just iterate in plain order. He *is* making use of a Hash property. He wants the keys to be unique. None of the other solutions shown in this thread have addressed that. James Edward Gray II