- 浏览: 852281 次
- 性别:
- 来自: 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 890I’m running on OSX, and using M ... -
Metaprogramming in Ruby: It’s All About the Self
2009-11-16 11:28 913After writing my last post on R ... -
A Teenage Boy Improved Ruby 1.9 Performance Up to 63%
2009-11-06 18:26 909Japanese online magazine, @IT J ... -
Ruby Best Practices - The Complete Class
2009-11-04 16:21 1050A remark: we enabled comment mo ... -
调查显示Ruby北美地区用户量上升
2009-10-30 07:38 588据Evans Data最近针对400名开发用户的调查表明,R ... -
RubyForge将停止工作,RubyGems.org接替Gem hosting服务
2009-10-28 14:14 1850Ruby Gem维护者请注意,数周前,GitHubGitHub ... -
有关Ruby企业版1.8.7的一些介绍
2009-10-18 08:54 1310前几周,Ruby企业版(Rub ... -
3 Ruby Quirks You Have to Love
2009-10-16 10:50 661Ruby’s a fantastic language; we ... -
Distilling JRuby: The JIT Compiler
2009-10-09 08:40 1147The JIT compiler in JRuby is a ... -
Compiling Ruby with MacRuby 0.5b1
2009-10-09 08:33 1280MacRuby 0.5b1 and can be downlo ... -
10月编程语言排行榜:Ruby稳步提升
2009-10-08 08:07 1015新闻来源:51CTO.COMTIOBE今日公布了2009年10 ... -
为你的.NET应用程序添加一个REPL控制台
2009-10-07 12:45 791微软开始推广IronPython和IronRuby,希望它们可 ... -
Installing Ruby 1.8 and 1.9 on Ubuntu from Source
2009-10-05 14:18 923$ sudo apt-get build-dep ru ... -
Compiling Ruby 1.9.1 (stable) on Ubuntu
2009-10-05 14:13 765I found the default ruby inst ... -
JRuby综述:1.4的新特性、JRubyConf议程及MLVM
2009-10-02 08:25 892JRuby 1.4 RC1即将发布,我们来看看新版本都有哪些新 ... -
Ruby Enterprise Edition 1.8.7-20090928 released
2009-09-30 08:09 1184In the not so distant past we l ... -
Ruby DCamp,低迷经济下别开生面的会议
2009-09-22 08:47 1002经济的低迷不仅影响了 ... -
Ruby静态分析工具检视:metric_fu, Simian, Saikuro以及其他
2009-09-17 08:59 1498代码质量构成了软件质 ... -
Ruby 1.9.1程序库兼容性纵览
2009-09-15 09:38 1150ruby.1.9.1是1.9.x系列第一 ... -
ruby 1.9 真有这么快吗?
2009-09-14 18:26 1199早在今年2月份,Antonio Cangiano 发表过一份关 ...
相关推荐
这个压缩包文件"生产规模的数据中心分析器CC Go Rust Python Java NodeJS PHP Ruby Perl.zip"包含了多种编程语言实现的数据中心分析器组件,这表明该工具可能支持跨平台和多语言集成。以下是对这些编程语言在数据...
尽管Perl曾是动态语言的首选,但现在已被Python和Ruby取代。Perl的面向对象机制复杂,学习曲线陡峭,而Python和Ruby则提供了更直观、更简洁的语法。对于需要大量正则表达式处理的特殊场景,Perl仍有其优势,但对于...
appium 示例代码(dotnet、java、node、perl、php、python、ruby 等)请参考https://github.com/appium/appium/tree/master/sample-code而不是此存储库示例代码该存储库包含主要用于 appium 功能测试的示例应用程序...
Ruby 是一种类似于 Python 和 Perl 的服务器端脚本语言。 Ruby 可以用来编写通用网关接口(CGI)脚本。 Ruby 可以被嵌入到超文本标记语言(HTML)。 Ruby 语法简单,这使得新的开发人员能够快速轻松地学习 Ruby。 ...
标题提到的“最好用的Lua,Python,Perl,Ruby,NSIS开发编辑器”显然是一款集成了多种编程语言支持的高效工具,它旨在为使用这些语言的开发者提供便利。这款编辑器可能包含了丰富的特性,如语法高亮、代码提示、调试...
我的经验是,通过实例来学习和教授 Python要比采取同样的方式去接触比方说 Ruby 或者 Perl 更加容易,因为 Python 的语法里面条条框框以及特殊的处理场景要少得多。 它所专注的并非语言表现的丰富程度,而是你想要用...
它结合了Perl的灵活性、Smalltalk的强大功能、Python的优雅以及C的效率,使其成为一种非常实用且强大的编程语言。Ruby的设计哲学强调代码的可读性和简洁性,这使得Ruby代码易于编写和维护。此外,Ruby支持多重继承、...
C 和 C++ 被公认为...SWIG 允许您向广泛的脚本语言公开 C/C++ 代码,包括 Ruby、Perl、Tcl 和 Python。本文使用 Ruby 作为公开 C/C++ 功能的首选脚本接口。要理解本文,您必须具备 C/C++ 与 Ruby 方面的相应知识。
在文档的行间散落着其他编程语言的名称,如Perl、Ruby、Java等,这可能意味着Python可以与这些语言进行交互或者提供了互操作性的支持。 8. Python在互联网技术中的应用 Python在文档中的提及和使用,显示了其在...
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-...
此外,该版本也可能对其他编程语言如Java、Python或Perl的互操作性进行了改进,以便更好地集成到多语言项目中。 总之,Ruby-2.4.5是Ruby语言的一个重要版本,为开发者提供了稳定且高效的编程环境。通过下载并安装这...
支持C.C++.Java.Objective-C.Perl.PHP.Python.Ruby.Shell和C#等代码的编写和运行, 比文本编辑器更强大,比专业类轻量 是你懂的 可用版
Ruby是一种简洁而强大的脚本语言,它融合了Perl的灵活性、Smalltalk的强大功能以及Python的优雅语法。其设计哲学强调代码的可读性和开发者的生产力,使得编写Ruby代码成为一种享受。Ruby语言的动态特性允许在运行时...
python-ruby-golang click(Python),thor(Ruby)和cli.go(Golang)的比较,用于构建非常简单的命令行工具。 快速开始 有关更多信息,请参见每个子目录中的README.md。 博客文章
与 Scheme、Ruby、Perl、Tcl 等动态语言一样,Python 具备垃圾回收功能,能够自动管理存储器使用。 Python 安装 Python 目前的版本已经更新到 3.4.0,本文使用的版本为 2.7.5,所有的版本都可以在 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