Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Perl/Ruby comparison

by Roy Johnson (Monsignor)
on Apr 12, 2005 at 12:39 UTC ( [id://446959]=note: print w/replies, xml ) Need Help??


in reply to Perl/Ruby comparison

There's a comparison here (by someone who wants to get away from Perl; you have to consider the source). His Perl could have been tightened up considerably. You can see many such comparisons if you Google perl ruby comparison or perl vs ruby.

I was impressed with a different benchmark, posted here in PM some time ago, that showed that Ruby generally has shorter source code for the same problem, with similar performance to Perl. I was further impressed with how quickly Ruby built when I downloaded and installed it. Unfortunately, I haven't had time to get into programming Ruby. It seems to me that Perl, Python, and Ruby all fill roughly the same niche. Preferences are generally based on style.


Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Perl/Ruby comparison
by JadeNB (Chaplain) on Dec 25, 2009 at 20:44 UTC
    His Perl could have been tightened up considerably.

    Boy, is that a call to action! I can't believe I'm the first in 4 years to have taken it up …. Here's a more-or-less faithful copy:

    use strict; use warnings 'all'; use constant { EMAIL => 17, CONTACTME => 27, SKUTITLE => 34 }; my @records; # Arrays are initialised empty; no need to do it explicit +ly. push @records, [ split "\t" ] for <>; do { tr/"//d for @$_ } for @records; my @contact_records; do { push @contact_records, [ @$_[ SKUTITLE, CONTACTME, EMAIL ] ] if $ +_->[CONTACTME] == 1 } for @records; { local $, = "\t"; local $\ = "\n"; print @$_ for sort { $a->[0] cmp $b->[0] } @contact_records; }

    To me, this task suffers, rather than benefits, from the names, and is much better suited for a Unix-style pipeline; fortunately, Perl is good at those (although I resort to substr to avoid the to-me unsatisfying s///; $_):

    use strict; use warnings 'all'; use constant { EMAIL => 17, CONTACTME => 27, SKUTITLE => 34 }; { local $, = "\t"; local $\ = "\n"; print @$_ for sort { $a->[0] cmp $b->[0] } grep { $_->[1] } map { chomp; [ map { substr $_, 1, -1 } ( split "\t" )[ SKUTITLE, CONTACTME, EMAIL ] ] } <>; }
    I've replaced the test for whether the CONTACTME field == 1 (not eq "1", ugh) with a test for whether it's true, since that's almost certainly what was meant; but it's easy to change it back.

    Finally, note that, since the CONTACTME field is not carrying any useful information, we might as well avoid printing it; so we could define

    sub snip_or_toss { $_[CONTACTME] ? @_[ SKUTITLE, EMAIL ] : () }
    replace the outer map by map { chomp; [ map {...} snip_or_toss split "\t" ] }, and then get rid of the grep.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://446959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-16 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found