From: djberg96@... (Daniel Berger) Date: 2004-03-04T00:34:44+09:00 Subject: Re: why are Hashes so unsorted? what's your solution? Ruby Baby wrote in message news:<20040303052244.GA89378@mail.hitmedia.com>... > I try to avoid questions like "Why doesn't Ruby do what ___ does?" > > But I'm stumped by hashes and asking for help to find a new solution. > > I use MySQL databases a lot. Let's say I have a query to find the > top-selling albums in my store: > > SELECT sku, artistname, albumtitle, sold FROM albums > ORDER BY sold DESC LIMIT 20; > > When I get the hash of results from the database, I'm used to making > the "sku" the hash key since it's the only unique identifier. > A perfect hash key. Used everywhere by my iteration methods. If you use DBI, it *is* the key. require "dbi" dbh = DBI.connect(dsn,user,passwd) sth = dbh.prepare("select sku, artistname, albumtitle, sold...") sth.execute while rec = sth.fetch puts "SKU: " + rec["SKU"] puts "Artist Name: " + rec["ARTISTNAME"] # etc, etc... end sth.finish dbh.disconnect Regards, Dan