`
love~ruby+rails
  • 浏览: 829925 次
  • 性别: Icon_minigender_1
  • 来自: lanzhou
社区版块
存档分类
最新评论

Perl vs. Python vs. Ruby

阅读更多

I’m evaluating Python and Ruby as replacements for Perl . I’ve been using Perl for several years and am very comfortable with it, although I’m definitely not an expert. Perl is a powerful language, but I think it’s ugly and encourages writing bad code, so I want to get rid of it. Python and Ruby both come with Mac OS X 10.2, both have BBEdit language modules, and both promise a cleaner approach to scripting. Over the past few weeks I read the Python Tutorial and the non-reference parts of Programming Ruby , however as of this afternoon I’d not written any Python or Ruby code yet.

Here’s a toy problem I wanted to solve. eSellerate gives me a tab-delimited file containing information about the people who bought my shareware . I wanted a script to extract from this file the e-mail addresses of people who asked to be contacted when I release the new versions of the products.

I decided to solve this problem in each language and then compare the resulting programs. The algorithm I chose was just the first one that came to mind. I coded it first in Ruby, and then ported the code to Python and Perl, changing it as little as possible. Thus, the style is perhaps not canonical Python or Perl, although since I’m new to Ruby it’s probably not canonical Ruby either. If I were just writing this in Perl, I might have tried to avoid Perl’s messy syntax for nested arrays and instead used an array of strings.

Here’s the basic algorithm:

  1. Read each line of standard input and break it into fields at each tab.
  2. Each field is wrapped in quotation marks, so remove them. Assume that there are no quotation marks in the interior of the field.
  3. Store the fields in an array called record .
  4. Create another array, records and fill it with all the record s.
  5. Make a new array, contactRecords , that contains arrays of just the fields we care about: SKUTITLE, CONTACTME, EMAIL.
  6. Sort contactRecords by SKUTITLE.
  7. Remove the elements of contactRecords where CONTACTME is not 1.
  8. Print contactRecords to standard output, with the fields separated by tabs and the records separated by newlines.

And here’s the code:

Perl

#!/usr/bin/perl -w

use strict;

my @records = ();

foreach my $line ( <> )
{
    my @record = map {s/"//g; $_} split("\t", $line);
    push(@records, \@record);
}

my $EMAIL = 17;
my $CONTACTME = 27;
my $SKUTITLE = 34;

my @contactRecords = ();
foreach my $r ( @records )
{
    push(@contactRecords, [$$r[$SKUTITLE], $$r[$CONTACTME], $$r[$EMAIL]]);
}

@contactRecords = sort {$$a[0] cmp $$b[0]} @contactRecords;
@contactRecords = grep($$_[1] eq "1", @contactRecords);

foreach my $r ( @contactRecords )
{
    print join("\t", @$r), "\n";
}

The punctuation and my ’s make this harder to read than it should be.

Python

#!/usr/bin/python

import fileinput

records = []

for line in fileinput.input():
    record = [field.replace('"', '') for field in line.split("\t")]
    records.append(record)

EMAIL = 17
CONTACTME = 27
SKUTITLE = 34

contactRecords = [[r[SKUTITLE], r[CONTACTME], r[EMAIL]] for r in records]
contactRecords.sort() # default sort will group by sku title
contactRecords = filter(lambda r: r[1] == "1", contactRecords)

for r in contactRecords:
    print "\t".join(r)

I think the Python version is generally the cleanest to read—that is, it’s the most English-like. I had to look up how join and filter worked, because they weren’t methods of list as I had guessed.

Ruby

#!/usr/bin/ruby

records = []

while gets
    record = $_.split('\t').collect! {|field| field.gsub('"', '') }
    records << record
end

EMAIL = 17
CONTACTME = 27
SKUTITLE = 34

contactRecords = records.collect {|r| [r[SKUTITLE], r[CONTACTME], r[EMAIL]] }
contactRecords.sort! # default sort will group by sku title
contactRecords.reject! {|a| a[1] != "1"}

contactRecords.each {|r|
    print r.join("\t"), "\n"
}

This is actually the shortest version, and I think it’s the easiest to read if you aren’t put off by the block syntax. I like how the sequence of operations in the first line of the while isn’t “backwards” as it is in the Perl and Python versions. Also, I correctly guessed which classes “owned” the methods and whether they were mutators.

分享到:
评论

相关推荐

    ruby-2.5.1.tar.gz

    Ruby 是一种类似于 Python 和 Perl 的服务器端脚本语言。 Ruby 可以用来编写通用网关接口(CGI)脚本。 Ruby 可以被嵌入到超文本标记语言(HTML)。 Ruby 语法简单,这使得新的开发人员能够快速轻松地学习 Ruby。 ...

    python练习题目.rar

    我的经验是,通过实例来学习和教授 Python要比采取同样的方式去接触比方说 Ruby 或者 Perl 更加容易,因为 Python 的语法里面条条框框以及特殊的处理场景要少得多。 它所专注的并非语言表现的丰富程度,而是你想要用...

    SWIG 公开 C/C++ 代码,包括 Ruby、Perl、Tcl、C# 和 Python

    C 和 C++ 被公认为...SWIG 允许您向广泛的脚本语言公开 C/C++ 代码,包括 Ruby、Perl、Tcl 和 Python。本文使用 Ruby 作为公开 C/C++ 功能的首选脚本接口。要理解本文,您必须具备 C/C++ 与 Ruby 方面的相应知识。

    python pywin32

    Python is often compared to Tcl, Perl, Ruby, Scheme or Java. Some of its key distinguishing features include: very clear, readable syntax strong introspection capabilities intuitive object ...

    QT6.2.4-webengine自编译,支持mp4等视频播放。这里删除了pdb。

    4.Perl、ruby 5.Node.js 建议12以上版本 6.Visual Studio 2019 7.Windows 10 SDK version 10.0.19041以上 vs2019自带 8.python3.8以上 编译qt源码使用3 9.ninja configure -prefix C:\x64release -release -force-...

    CodeRunner for Mac 2.2.2

    支持C.C++.Java.Objective-C.Perl.PHP.Python.Ruby.Shell和C#等代码的编写和运行, 比文本编辑器更强大,比专业类轻量 是你懂的 可用版

    最好用的Lua,Python,Perl,Ruby,NSIS开发编辑器

    最好用的Lua脚本开发编辑器还支持Python,Perl,Ruby,NSIS

    pro_apache_third_edition..pdf

    Contents About the Author...............................................................................................xix About the Technical Reviewer and Contributing Author.................xxi ...

    python-ruby-golang:比较python,ruby和golang

    python-ruby-golang click(Python),thor(Ruby)和cli.go(Golang)的比较,用于构建非常简单的命令行工具。 快速开始 有关更多信息,请参见每个子目录中的README.md。 博客文章

    Python对Excel操作教程.doc

    与Scheme、Ruby、Perl、Tcl等动态语言一样,Python具备垃圾回收功能,能够自动 管理存储器使用。它经常被当作脚本语言用于处理系统管理任务和网络程序编写,然而 它也非常适合完成各种高级任务。Python虚拟机本身...

    ruby编程学习笔记及demo

    Ruby 的特性与 Smalltalk、Perl 和 Python 类似。Perl、Python 和 Smalltalk 是脚本语言。Smalltalk 是一个真正的面向对象语言。Ruby,与 Smalltalk 一样,是一个完美的面向对象语言。使用 Ruby 的语法比使用 ...

    代码高亮工具CodeHighLight

    [代码高亮工具] ... ... 支持主流的20种编程语言的高亮显示 * 1.... * 2.... * 3.... Ruby/Rails * 13. Perl * 14. Assembly * 15. Bat 批处理 * 16. UNIX Shell * 18. AWK * 19. Sql * 20. xml/xhtml

    15天精通Python-Python总结-Python基础-pyhthon小代码.docx

    15天精通Python-Python总结-Python基础-pyhthon小代码 Python总结 引言 语言分类 机器语言—汇编语言—高级语言(C语言—java/python)--php/perl/ruby C语言—面向过程 Java/python—面向对象 Python 是面向过程、...

    Programming Ruby

    Like Smalltalk, it is dynamically typed (as opposed to Java or C++), but unlike Smalltalk, Ruby features the same conveniences found in modern scripting languages such as Perl and Python. The ...

    Programming Ruby 1.9 & 2.0 The Pragmatic Programmers’ Guide 4th Edition

    Like Smalltalk, it is dynamically typed (as opposed to Java or C++), but unlike Smalltalk, Ruby features the same conveniences found in modern scripting languages such as Perl and Python. The ...

    ruby-libs-2.0.0.648-35.el7_6.i686.rpm

    Ruby 是一种类似于 Python 和 Perl 的服务器端脚本语言。 Ruby 可以用来编写通用网关接口(CGI)脚本。 Ruby 可以被嵌入到超文本标记语言(HTML)。 Ruby 语法简单,这使得新的开发人员能够快速轻松地学习 Ruby。 ...

    《activmq in action 》

    I. An Introduction to Messaging and ActiveMQ ............................................. 1 1. Introduction to Apache ActiveMQ ..........................................................

    Python参考手册(第4版·修订版).[美]David M.Beazley(带详细书签)

    本书是Python编程语言的杰出参考手册,书中详尽讲解了Python核心和Python库...他创办的Dabeaz 公司提供软件开发、培训和咨询服务,专注于Python、Ruby、Perl 等动态编程语言的实际应用。他是Python 软件基金会的会员。

    gvim常用插件及其配置文件配置(下载解压即可使用)

    html.snippets objc.snippets ruby.snippets tcl.snippets .vim/syntax: c.vim doxygen.vim HGAnnotate.vim snippet.vim SVNAnnotate.vim CVSAnnotate.vim gitAnnotate.vim python.vim SVKAnnotate....

    The Ruby Way

    The design philosophy of Ruby encourages human-oriented design, rapid development, and test-first coding. &lt;br/&gt;Use Ruby where you would use Perl, C++,Smalltalk or Python, but free from special ...

Global site tag (gtag.js) - Google Analytics