On Dec 9, 2005, at 11:23 AM, pat eyler wrote: > On 12/9/05, Sam Dela Cruz <sam.dela.cruz / philips.com> wrote: >> Hi, >> >> I'm starting to use Ruby in one of my projects at work. I'm >> coming from a >> Perl background. >> In my project I would need to parse a list of numbers (thousands >> of them) >> and then return the duplicates. In perl, I can do this: >> > [elided perl goo] >> >> I tried to translate this in Ruby, but could not find en >> equivalent of >> $hash{$_}++, this is auto increment. >> Can somebody tell me how this is to be done in Ruby? > > translating from Perl to Ruby seems often to be a bad idea ... a > common idea, but not necessarily a good one. I'd rather work > with a Ruby solution to a problem than a Rubification of a Perl > solution to a problem. Ditto. I often find that I can make my code close to readable English and find that a very good thing. >> Or maybe the Ruby way on how to attack this whole thing. Thanks. > > I'm assuming that your list comes from a file (but you can change that > pretty easily in the code below), given that, how about something > like: > > seen_ary = Array.new > > File.open("nums","r").each do |elem| > print elem if seen_ary.include?(elem) > seen_ary.push(elem) > end > > (there are probably still better ways of doing this though) I'll go with: seen = {} ARGF.each do |elem| print elem if seen.include? elem seen[elem] = true end -- Eric Hodel - drbrain / segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com