Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby vs Python vs Perl (programming example)

Reply
Thread Tools

Ruby vs Python vs Perl (programming example)

 
 
Laura Raffa
Guest
Posts: n/a
 
      03-01-2005
Hi,

I have no experience with Ruby, but there is currently a debate in the
office about Python over Perl, Java, C++ etc. It's a simple programming
example:

find and print all words ending with some predefined suffix

An array of words can be assumed. The shortest possible code solution (and
also the most elegant) is required. A Perl solution with the -n option was
disallowed.

As I don't know Ruby, can anyone give me a quick example. The Ruby guy who
rabbits on about the excellence of Ruby isn't here to day. Thought I'd
check.


Thanks


 
Reply With Quote
 
 
 
 
Assaph Mehr
Guest
Posts: n/a
 
      03-01-2005
> find and print all words ending with some predefined suffix
>
> An array of words can be assumed. The shortest possible code solution

(and
> also the most elegant) is required. A Perl solution with the -n

option was
> disallowed.


Assume:
.. words = [...] # array
.. suffix = "..." # string

Then:
.. puts words.select { |word| word =~ /#{suffix}$/ }

 
Reply With Quote
 
 
 
 
Dave Burt
Guest
Posts: n/a
 
      03-02-2005

"Laura Raffa" <(E-Mail Removed)> wrote:
> Hi,
>
> I have no experience with Ruby, but there is currently a debate in the
> office about Python over Perl, Java, C++ etc. It's a simple programming
> example:
>
> find and print all words ending with some predefined suffix
>
> An array of words can be assumed. The shortest possible code solution (and
> also the most elegant) is required. A Perl solution with the -n option was
> disallowed.
>


Ruby:
puts words.select { |word| /#{suffix}$/ =~ word }
or
words.each {|word| puts word if /#{suffix}$/ =~ word }
or
for word in words
puts word if /#{suffix}$/ =~ word
end

Perl:
print grep /$suffix$/, @words;
or
for (@words) { print if /$suffix$/ }

I think I prefer Perl's solution for such a trivial example. You're only
going to see the superiority of Ruby over Perl in a bigger problem, so you
can crack out your elegant class definitions and reflection and general
dynamism, and watch the Perl hacker fight with references and {$%@#/&***/\

Cheers,
Dave


 
Reply With Quote
 
Csaba Henk
Guest
Posts: n/a
 
      03-02-2005
On 2005-03-01, Assaph Mehr <(E-Mail Removed)> wrote:
>> find and print all words ending with some predefined suffix
>>
>> An array of words can be assumed. The shortest possible code solution

> (and
>> also the most elegant) is required. A Perl solution with the -n

> option was
>> disallowed.

>
> Assume:
> . words = [...] # array
> . suffix = "..." # string
>
> Then:
> . puts words.select { |word| word =~ /#{suffix}$/ }


Maybe /#{Regexp.quote suffix}$/, if you want to be general...

Csaba
 
Reply With Quote
 
gabriele renzi
Guest
Posts: n/a
 
      03-02-2005
Assaph Mehr ha scritto:
>>find and print all words ending with some predefined suffix
>>
>>An array of words can be assumed. The shortest possible code solution

>
> (and
>
>>also the most elegant) is required. A Perl solution with the -n

>
> option was
>
>>disallowed.

>
>
> Assume:
> . words = [...] # array
> . suffix = "..." # string
>
> Then:
> . puts words.select { |word| word =~ /#{suffix}$/ }


for String there is a special method of Enumerable, since this is an
important case:
puts words.grep(/suffix$/)
 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      03-02-2005

"gabriele renzi" <(E-Mail Removed)> ha scritto:
> Assaph Mehr ha scritto:
>> . puts words.select { |word| word =~ /#{suffix}$/ }

>
> for String there is a special method of Enumerable, since this is an
> important case:
> puts words.grep(/suffix$/)


Cool! It's not a special case, it's a case-equality match:

-------------------------------------------------------- Enumerable#grep
enum.grep(pattern) => array
enum.grep(pattern) {| obj | block } => array
------------------------------------------------------------------------
Returns an array of every element in _enum_ for which +Pattern ===
element+. If the optional _block_ is supplied, each matching
element is passed to it, and the block's result is stored in the
output array.

a = [nil, 1, 3.5, 'a', nil, 10**100, [], {}, 'b', 'c']
a.grep(NilClass).size #=> 2
a.grep(String) #=> ['a', 'b', 'c']
a.grep(Numeric) #=> [1, 3.5, 10000...]

So you can do regex-matches on string arrays, and kind_of?-matches, and
equality-matches.

Cheers,
Dave


 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      03-02-2005
"Laura Raffa" <(E-Mail Removed)> wrote in message news:<AE6Vd.11$(E-Mail Removed)>...
>
> find and print all words ending with some predefined suffix


puts words.grep(/#{suffix}$/)

Regards,
Pit
 
Reply With Quote
 
gabriele renzi
Guest
Posts: n/a
 
      03-02-2005
Dave Burt ha scritto:
> "gabriele renzi" <(E-Mail Removed)> ha scritto:
>
>>Assaph Mehr ha scritto:
>>
>>>. puts words.select { |word| word =~ /#{suffix}$/ }

>>
>>for String there is a special method of Enumerable, since this is an
>>important case:
>> puts words.grep(/suffix$/)

>
>
> Cool! It's not a special case, it's a case-equality match:


Well, I think that is the generalization of the special case
as the name suggest, I think it was initially done for finding strings
 
Reply With Quote
 
Guyver2
Guest
Posts: n/a
 
      03-02-2005
"Laura Raffa" <(E-Mail Removed)> wrote in message news:<AE6Vd.11$(E-Mail Removed)>...
> Hi,
>
> I have no experience with Ruby, but there is currently a debate in the
> office about Python over Perl, Java, C++ etc. It's a simple programming
> example:
>
> find and print all words ending with some predefined suffix
>
> An array of words can be assumed. The shortest possible code solution (and
> also the most elegant) is required. A Perl solution with the -n option was
> disallowed.
>
> As I don't know Ruby, can anyone give me a quick example. The Ruby guy who
> rabbits on about the excellence of Ruby isn't here to day. Thought I'd
> check.
>
>
> Thanks


http://www.approximity.com/ruby/Comp...st_m_java.html
http://www.angelfire.com/tx4/cus/shapes/
 
Reply With Quote
 
Kristof Bastiaensen
Guest
Posts: n/a
 
      03-02-2005
On Wed, 02 Mar 2005 00:32:05 -0800, Pit Capitain wrote:

> "Laura Raffa" <(E-Mail Removed)> wrote in message news:<AE6Vd.11$(E-Mail Removed)>...
>>
>> find and print all words ending with some predefined suffix

>
> puts words.grep(/#{suffix}$/)
>
> Regards,
> Pit


Nice. If suffix contains regexp characters it is safer to quote them:

puts words.grep(/#{Regexp.quote(suffix)}$/)

Regards,
KB


 
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
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Ruby 2.0 joint Ruby/Perl/Python backend ? surf Ruby 9 01-18-2007 08:16 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments