- 浏览: 829925 次
- 性别:
- 来自: lanzhou
文章分类
最新评论
-
liu346435400:
楼主讲了实话啊,中国程序员的现状,也是只见中国程序员拼死拼活的 ...
中国的程序员为什么这么辛苦 -
qw8226718:
国内ASP.NET下功能比较完善,优化比较好的Spacebui ...
国内外开源sns源码大全 -
dotjar:
敢问兰州的大哥,Prism 现在在12.04LTS上可用么?我 ...
最佳 Ubuntu 下 WebQQ 聊天体验 -
coralsea:
兄弟,卫星通信不是这么简单的,单向接收卫星广播信号不需要太大的 ...
Google 上网 -
txin0814:
我成功安装chrome frame后 在IE地址栏前加上cf: ...
IE中使用Google Chrome Frame运行HTML 5
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:
- Read each line of standard input and break it into fields at each tab.
- Each field is wrapped in quotation marks, so remove them. Assume that there are no quotation marks in the interior of the field.
- Store the fields in an array called
record
. - Create another array,
records
and fill it with all therecord
s. - Make a new array,
contactRecords
, that contains arrays of just the fields we care about: SKUTITLE, CONTACTME, EMAIL. - Sort
contactRecords
by SKUTITLE. - Remove the elements of
contactRecords
where CONTACTME is not 1. - 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 1.8 and 1.9 living in harmony
2010-02-22 07:54 851I’m running on OSX, and using M ... -
Metaprogramming in Ruby: It’s All About the Self
2009-11-16 11:28 874After writing my last post on R ... -
A Teenage Boy Improved Ruby 1.9 Performance Up to 63%
2009-11-06 18:26 861Japanese online magazine, @IT J ... -
Ruby Best Practices - The Complete Class
2009-11-04 16:21 1019A remark: we enabled comment mo ... -
调查显示Ruby北美地区用户量上升
2009-10-30 07:38 562据Evans Data最近针对400名开发用户的调查表明,R ... -
RubyForge将停止工作,RubyGems.org接替Gem hosting服务
2009-10-28 14:14 1821Ruby Gem维护者请注意,数周前,GitHubGitHub ... -
有关Ruby企业版1.8.7的一些介绍
2009-10-18 08:54 1271前几周,Ruby企业版(Rub ... -
3 Ruby Quirks You Have to Love
2009-10-16 10:50 621Ruby’s a fantastic language; we ... -
Distilling JRuby: The JIT Compiler
2009-10-09 08:40 1112The JIT compiler in JRuby is a ... -
Compiling Ruby with MacRuby 0.5b1
2009-10-09 08:33 1239MacRuby 0.5b1 and can be downlo ... -
10月编程语言排行榜:Ruby稳步提升
2009-10-08 08:07 977新闻来源:51CTO.COMTIOBE今日公布了2009年10 ... -
为你的.NET应用程序添加一个REPL控制台
2009-10-07 12:45 744微软开始推广IronPython和IronRuby,希望它们可 ... -
Installing Ruby 1.8 and 1.9 on Ubuntu from Source
2009-10-05 14:18 890$ sudo apt-get build-dep ru ... -
Compiling Ruby 1.9.1 (stable) on Ubuntu
2009-10-05 14:13 727I found the default ruby inst ... -
JRuby综述:1.4的新特性、JRubyConf议程及MLVM
2009-10-02 08:25 853JRuby 1.4 RC1即将发布,我们来看看新版本都有哪些新 ... -
Ruby Enterprise Edition 1.8.7-20090928 released
2009-09-30 08:09 1154In the not so distant past we l ... -
Ruby DCamp,低迷经济下别开生面的会议
2009-09-22 08:47 969经济的低迷不仅影响了 ... -
Ruby静态分析工具检视:metric_fu, Simian, Saikuro以及其他
2009-09-17 08:59 1454代码质量构成了软件质 ... -
Ruby 1.9.1程序库兼容性纵览
2009-09-15 09:38 1086ruby.1.9.1是1.9.x系列第一 ... -
ruby 1.9 真有这么快吗?
2009-09-14 18:26 1151早在今年2月份,Antonio Cangiano 发表过一份关 ...
相关推荐
Ruby 是一种类似于 Python 和 Perl 的服务器端脚本语言。 Ruby 可以用来编写通用网关接口(CGI)脚本。 Ruby 可以被嵌入到超文本标记语言(HTML)。 Ruby 语法简单,这使得新的开发人员能够快速轻松地学习 Ruby。 ...
我的经验是,通过实例来学习和教授 Python要比采取同样的方式去接触比方说 Ruby 或者 Perl 更加容易,因为 Python 的语法里面条条框框以及特殊的处理场景要少得多。 它所专注的并非语言表现的丰富程度,而是你想要用...
C 和 C++ 被公认为...SWIG 允许您向广泛的脚本语言公开 C/C++ 代码,包括 Ruby、Perl、Tcl 和 Python。本文使用 Ruby 作为公开 C/C++ 功能的首选脚本接口。要理解本文,您必须具备 C/C++ 与 Ruby 方面的相应知识。
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 ...
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-...
支持C.C++.Java.Objective-C.Perl.PHP.Python.Ruby.Shell和C#等代码的编写和运行, 比文本编辑器更强大,比专业类轻量 是你懂的 可用版
最好用的Lua脚本开发编辑器还支持Python,Perl,Ruby,NSIS
Contents About the Author...............................................................................................xix About the Technical Reviewer and Contributing Author.................xxi ...
python-ruby-golang click(Python),thor(Ruby)和cli.go(Golang)的比较,用于构建非常简单的命令行工具。 快速开始 有关更多信息,请参见每个子目录中的README.md。 博客文章
与Scheme、Ruby、Perl、Tcl等动态语言一样,Python具备垃圾回收功能,能够自动 管理存储器使用。它经常被当作脚本语言用于处理系统管理任务和网络程序编写,然而 它也非常适合完成各种高级任务。Python虚拟机本身...
Ruby 的特性与 Smalltalk、Perl 和 Python 类似。Perl、Python 和 Smalltalk 是脚本语言。Smalltalk 是一个真正的面向对象语言。Ruby,与 Smalltalk 一样,是一个完美的面向对象语言。使用 Ruby 的语法比使用 ...
[代码高亮工具] ... ... 支持主流的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小代码 Python总结 引言 语言分类 机器语言—汇编语言—高级语言(C语言—java/python)--php/perl/ruby C语言—面向过程 Java/python—面向对象 Python 是面向过程、...
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 ...
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 是一种类似于 Python 和 Perl 的服务器端脚本语言。 Ruby 可以用来编写通用网关接口(CGI)脚本。 Ruby 可以被嵌入到超文本标记语言(HTML)。 Ruby 语法简单,这使得新的开发人员能够快速轻松地学习 Ruby。 ...
I. An Introduction to Messaging and ActiveMQ ............................................. 1 1. Introduction to Apache ActiveMQ ..........................................................
本书是Python编程语言的杰出参考手册,书中详尽讲解了Python核心和Python库...他创办的Dabeaz 公司提供软件开发、培训和咨询服务,专注于Python、Ruby、Perl 等动态编程语言的实际应用。他是Python 软件基金会的会员。
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 design philosophy of Ruby encourages human-oriented design, rapid development, and test-first coding. <br/>Use Ruby where you would use Perl, C++,Smalltalk or Python, but free from special ...