Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > new to Ruby - pls help in translating this

Reply
Thread Tools

new to Ruby - pls help in translating this

 
 
Sam Dela Cruz
Guest
Posts: n/a
 
      12-09-2005
--=_alternative 006751FE882570D2_=
Content-Type: text/plain; charset="US-ASCII"

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:

##### Perl code
%hash = {};
while (<>)
{
chomp;
$hash{$_}++;
}

foreach my $key (sort keys %hash)
{
print "$key: $hash{$key}\n" if ($hash{$key} > 1);
}

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? Or maybe the Ruby
way on how to attack this whole thing. Thanks.

Regards,
Sam
--=_alternative 006751FE882570D2_=--


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      12-09-2005
On Dec 9, 2005, at 12:48 PM, Sam Dela Cruz 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:
>
> ##### Perl code
> %hash = {};
> while (<>)
> {
> chomp;
> $hash{$_}++;
> }
>
> foreach my $key (sort keys %hash)
> {
> print "$key: $hash{$key}\n" if ($hash{$key} > 1);
> }
>
> I tried to translate this in Ruby, but could not find en equivalent of
> $hash{$_}++, this is auto increment.


See if this gets you going:

seen = Hash.new(0)
ARGF.each_line { |line| seen[line.strip] += 1 }

seen.each { |key, value| puts key if value > 1 }

James Edward Gray II


 
Reply With Quote
 
 
 
 
Dan Diebolt
Guest
Posts: n/a
 
      12-09-2005
--0-111865063-1134155277=:55437
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

use the following to initiate your hash:
=20
h=3DHash.new(0)
=20
and this to increment on each key:
=20
h[key] =3D h[key] + 1

=09
---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping=20
--0-111865063-1134155277=:55437--


 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      12-09-2005
--nextPart2593319.70Ojd7Xiaq
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hey,

after looking at your code (wich was a bit hard, since i don't speak perl) =
=2D i=20
came up with following solution, that doesn't seem very ruby-esque but work=
s=20
well for me.
I'm sure someone brings the code down to one line, but i'm a bit too lazy t=
o=20
try that

file =3D File.open(ARGV[0]).readlines
h =3D {}
file.each{|x| (h[x].nil?) ? (h[x] =3D 1) : (h[x] =3D h[x]+=3D1)}
h.each_pair{|k,v| puts "#{k.strip} appears #{v} times" if v >=3D 2}

Am Freitag, 9. Dezember 2005 19:48 schrieb Sam Dela Cruz:
> 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:
>
> ##### Perl code
> %hash =3D {};
> while (<>)
> {
> chomp;
> $hash{$_}++;
> }
>
> foreach my $key (sort keys %hash)
> {
> print "$key: $hash{$key}\n" if ($hash{$key} > 1);
> }
>
> 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? Or maybe the Ruby
> way on how to attack this whole thing. Thanks.
>
> Regards,
> Sam


--nextPart2593319.70Ojd7Xiaq
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDmdd8MdQeL6eBxhIRAjkCAJ9Xl6Gcu+jPkn+Kkaksxz LLd8yhywCg0dkd
RpwC7Xt6ZUMuJKk8SEfxaAU=
=xeN0
-----END PGP SIGNATURE-----

--nextPart2593319.70Ojd7Xiaq--


 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      12-09-2005

On Dec 9, 2005, at 1:48 PM, Sam Dela Cruz 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:
>
> ##### Perl code
> %hash = {};
> while (<>)
> {
> chomp;
> $hash{$_}++;
> }
>
> foreach my $key (sort keys %hash)
> {
> print "$key: $hash{$key}\n" if ($hash{$key} > 1);
> }
>


hash = Hash.new { |h, k| h[k] = 0 }

while gets
$_.chomp!
hash[$_] += 1
end

> I tried to translate this in Ruby, but could not find en equivalent of
> $hash{$_}++, this is auto increment.


Ruby has no auto-increment since variables are more like labels on
objects than containers for objects and numbers are generally
immutable in ruby.
IOW:
x = 3
x++ in ruby would be like typing 3++ which doesn't make sense really.

> Can somebody tell me how this is to be done in Ruby? Or maybe the
> Ruby
> way on how to attack this whole thing. Thanks.
>
> Regards,
> Sam




 
Reply With Quote
 
pat eyler
Guest
Posts: n/a
 
      12-09-2005
On 12/9/05, Sam Dela Cruz <(E-Mail Removed)> 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.

> 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 =3D 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)

>
> Regards,
> Sam
>



--
thanks,
-pate
-------------------------


 
Reply With Quote
 
JB Eriksson
Guest
Posts: n/a
 
      12-09-2005
------=_Part_10186_18470900.1134156277131
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

well, there is the succ method. and there's a succ! on String, which could
work in this case, I guess(?), since:

irb(main):023:0> a =3D "9"
=3D> "9"
irb(main):024:0> a.succ!
=3D> "10"


On 12/9/05, Logan Capaldo <(E-Mail Removed)> wrote:
>
>
> On Dec 9, 2005, at 1:48 PM, Sam Dela Cruz 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:
> >
> > ##### Perl code
> > %hash =3D {};
> > while (<>)
> > {
> > chomp;
> > $hash{$_}++;
> > }
> >
> > foreach my $key (sort keys %hash)
> > {
> > print "$key: $hash{$key}\n" if ($hash{$key} > 1);
> > }
> >

>
> hash =3D Hash.new { |h, k| h[k] =3D 0 }
>
> while gets
> $_.chomp!
> hash[$_] +=3D 1
> end
>
> > I tried to translate this in Ruby, but could not find en equivalent of
> > $hash{$_}++, this is auto increment.

>
> Ruby has no auto-increment since variables are more like labels on
> objects than containers for objects and numbers are generally
> immutable in ruby.
> IOW:
> x =3D 3
> x++ in ruby would be like typing 3++ which doesn't make sense really.
>
> > Can somebody tell me how this is to be done in Ruby? Or maybe the
> > Ruby
> > way on how to attack this whole thing. Thanks.
> >
> > Regards,
> > Sam

>
>
>


------=_Part_10186_18470900.1134156277131--


 
Reply With Quote
 
Dan Diebolt
Guest
Posts: n/a
 
      12-09-2005
--0-462817583-1134156660=:31051
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

>variables are more like labels on objects than containers for objects=20

=20
There is something very fundamental about what you are saying here but =
I don't quite understand. Could you elaborate on this with perhaps an exa=
mple?

=09
---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping=20
--0-462817583-1134156660=:31051--


 
Reply With Quote
 
Wilson Bilkovich
Guest
Posts: n/a
 
      12-09-2005
Or, something else to think about.. shamelessly borrowed from this
mailing list a month or so ago..
Wrapped in lame 'gets' code to let you play with it at the command
line. I presume you'd actually do something cooler with STDIN or a
filename.

class Array
def dups
h, d =3D {}, []; each{|e| h[e] ? (d << h.delete(e)) : (h[e] =3D e)}; d
end
end
numbers =3D []
while n =3D STDIN.gets.chomp
break if n =3D=3D ''
numbers << n
end
puts numbers.dups

I love reopening Array.

--Wilson.

On 12/9/05, Sam Dela Cruz <(E-Mail Removed)> 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:
>
> ##### Perl code
> %hash =3D {};
> while (<>)
> {
> chomp;
> $hash{$_}++;
> }
>
> foreach my $key (sort keys %hash)
> {
> print "$key: $hash{$key}\n" if ($hash{$key} > 1);
> }
>
> 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? Or maybe the Ruby
> way on how to attack this whole thing. Thanks.
>
> Regards,
> Sam
>



 
Reply With Quote
 
Eric Hodel
Guest
Posts: n/a
 
      12-09-2005
On Dec 9, 2005, at 11:23 AM, pat eyler wrote:

> On 12/9/05, Sam Dela Cruz <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help translating C# socket code equivalent Ruby socket code Kamaljeet Saini Ruby 2 01-30-2009 04:02 AM
Need help translating Ruby code Kevin Tran Ruby 1 09-09-2008 02:53 PM
URGENT - Pls help...pls recommend - laptop purchase irfansmith@gmail.com Computer Information 2 08-15-2008 11:34 PM
Translating artifical intelligence examples from C to Ruby Tom Copeland Ruby 0 10-16-2003 03:38 PM
pls, help.. i need a number..pls olabanji timothy MCSE 7 09-10-2003 04:02 PM



Advertisments