On Dec 9, 2005, at 2:53 PM, Sam Dela Cruz wrote:

> Here's what I decided on using (looks more like what I originally  
> posted
> in perl):
>
> hash = Hash.new(0)
> File.open(ARGV[0],"r").each do |line|

Replacing the above with:

ARGF.each do |line|

Is the same thing, but more powerful.  You could feed it multiple  
files at once, or use it in pipelined processing.  Just FYI.

James Edward Gray II

>     hash[line.chomp] += 1
> end
>
> hash.keys.sort.each do |key|
>     puts "#{key}: #{hash[key]}" if hash[key] > 1
> end
>
> Regards,
> Sam Dela Cruz
>
>
>
>
>
>
>
>
>
> Eric Hodel <drbrain / segment7.net>
> 12/09/2005 11:39 AM
> Please respond to
> ruby-talk / ruby-lang.org
>
>
> To
> ruby-talk / ruby-lang.org (ruby-talk ML)
> cc
>
> Subject
> Re: new to Ruby - pls help in translating this
> Classification
>
>
>
>
>
>
>
> 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
>
>
>
>