`
love~ruby+rails
  • 浏览: 852404 次
  • 性别: 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.

分享到:
评论

相关推荐

    生产规模的数据中心分析器CC Go Rust Python Java NodeJS PHP Ruby Perl.zip

    这个压缩包文件"生产规模的数据中心分析器CC Go Rust Python Java NodeJS PHP Ruby Perl.zip"包含了多种编程语言实现的数据中心分析器组件,这表明该工具可能支持跨平台和多语言集成。以下是对这些编程语言在数据...

    为什么你一定要学习Python或Ruby语言.pdf

    尽管Perl曾是动态语言的首选,但现在已被Python和Ruby取代。Perl的面向对象机制复杂,学习曲线陡峭,而Python和Ruby则提供了更直观、更简洁的语法。对于需要大量正则表达式处理的特殊场景,Perl仍有其优势,但对于...

    appium 示例代码(dotnet、java、node、perl、php、python、ruby 等).zip

    appium 示例代码(dotnet、java、node、perl、php、python、ruby 等)请参考https://github.com/appium/appium/tree/master/sample-code而不是此存储库示例代码该存储库包含主要用于 appium 功能测试的示例应用程序...

    ruby-2.5.1.tar.gz

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

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

    标题提到的“最好用的Lua,Python,Perl,Ruby,NSIS开发编辑器”显然是一款集成了多种编程语言支持的高效工具,它旨在为使用这些语言的开发者提供便利。这款编辑器可能包含了丰富的特性,如语法高亮、代码提示、调试...

    python练习题目.rar

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

    Programming Ruby.pdf

    它结合了Perl的灵活性、Smalltalk的强大功能、Python的优雅以及C的效率,使其成为一种非常实用且强大的编程语言。Ruby的设计哲学强调代码的可读性和简洁性,这使得Ruby代码易于编写和维护。此外,Ruby支持多重继承、...

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

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

    使用Python进行socket编程.pdf

    在文档的行间散落着其他编程语言的名称,如Perl、Ruby、Java等,这可能意味着Python可以与这些语言进行交互或者提供了互操作性的支持。 8. Python在互联网技术中的应用 Python在文档中的提及和使用,显示了其在...

    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-...

    ruby-2.4.5.tar.gz

    此外,该版本也可能对其他编程语言如Java、Python或Perl的互操作性进行了改进,以便更好地集成到多语言项目中。 总之,Ruby-2.4.5是Ruby语言的一个重要版本,为开发者提供了稳定且高效的编程环境。通过下载并安装这...

    CodeRunner for Mac 2.2.2

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

    Ruby.Programming.Language.The.Jan.2008

    Ruby是一种简洁而强大的脚本语言,它融合了Perl的灵活性、Smalltalk的强大功能以及Python的优雅语法。其设计哲学强调代码的可读性和开发者的生产力,使得编写Ruby代码成为一种享受。Ruby语言的动态特性允许在运行时...

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

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

    最新Python对Excel操作教程.docx

    与 Scheme、Ruby、Perl、Tcl 等动态语言一样,Python 具备垃圾回收功能,能够自动管理存储器使用。 Python 安装 Python 目前的版本已经更新到 3.4.0,本文使用的版本为 2.7.5,所有的版本都可以在 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

Global site tag (gtag.js) - Google Analytics