From: Jean-Denis Muys Date: 2009-02-06T23:58:43+09:00 Subject: Re: Question - looking for library to use for query like code? Quoting Mmcolli00 Mom : > Can this be done with Ruby? (this may be a shot in the dark) > > Say you have a text file with a lot of data and you want to group all of > your data by a specific value. Is there a way to code something in ruby > similar to a query that you would create using sql. Can this even be > done using Ruby? And if so, is there a library that you can refer me to? > If not, do you know any alternative ways? Thanks, MC > > example of text: > > auto, model > car, honda > truck, chevy > car, nissan > truck, toyota > truck, ford > > > how i'd like it to look: > > car, honda, nissan, > truck, chevy, toyota, ford > -- > Posted via http://www.ruby-forum.com/. > > Of course this is possible, and rather easy to write. Here are bits I would use: cars = hash.new file = File.open("textfile.txt") file.each { | line | wordarray = line.split(/,\s*/) cars[wordarray[0]] << wordarray[1] } file.close file = File.open("outfile.txt", "w") file << "cars, " cars["cars"].each { |car| file << car << ", " } file << "\n" cars.["truck"].each { |car| file << car << ", " } file << "\n" file.close This is not a complete, nor a general solution, but this should give you an idea. Jean-Denis