From: Gavin Sinclair Date: 2002-08-27T12:02:26+09:00 Subject: Re: Hybrid hash and array? >> [Phillip:] >> When programming radio buttons on a website, I find myself using this >> sort of construct a lot: >> >> @choices = [ >> ['1', 'Spring'], >> ['2', 'Summer'], >> ['3', 'Fall'] >> ] >> >> @table = {} >> @choices.each do |key, value| >> @table[key] = value >> end >> >> The data structure I really want is like a hash, except that it >> maintains its key/value pairs in the order that I defined them. > [Hal:] > You could always use Array#assoc... > > @choices.assoc('1')[1] # 'Spring' > @choices.assoc('3')[1] # 'Fall' > [Gavin:] You can, but it's ugly. Like Phillip, I have occasionally wanted to retrieve hash pairs in insertion order. It seems such a simple request. It's not something I expect of the language, but a module would be nice. Perl has IxHash for this. I know absolutely nothing else about it, but maybe I'll implement it! (Maybe it already exists on RAA, but I haven't seen it.) By the way, to convert array = [[a,1], [c,2], ...] to a hash, you can use: (hang on, I thought there was a built-in way to do it in one expression - I must be wrong) Gavin