Thank you all. I learned a little bit more ruby. And yes, I want to do
things the Ruby way, but I just can't help to think perl still and then
translate to ruby. This is very similar to when I came to the U.S. and
all of a sudden I have to talk in English, I still think in my original
language(Tagalog) and then translate it in English and it doesn't always
come up the way English speakers would say it. But I get the point
accross. But yes, going back to Ruby, I'd like to learn things the Ruby
way and so far I have good things to say about Ruby. One, it being so
readable, and second I think it is easier to learn especially the OOP part
of it. In perl OOP is a big hurdle.
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|
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
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
···
On Dec 9, 2005, at 11:23 AM, pat eyler wrote:
On 12/9/05, Sam Dela Cruz <sam.dela.cruz@philips.com> wrote:
--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant
http://trackmap.robotcoop.com