设为首页 收藏本站
查看: 579|回复: 0

[经验分享] Perl vs. Python vs. Ruby

[复制链接]

尚未签到

发表于 2017-4-26 06:00:07 | 显示全部楼层 |阅读模式
  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 the record
    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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-369190-1-1.html 上篇帖子: Python语法小全 下篇帖子: 使用Python处理XML
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表