A Mountain of Perl Books + Python Advocacy

David Bolen db3l at fitlinxx.com
Mon May 8 19:11:22 EDT 2000


tsummerfelt1 at myspleenhome.com (tony summerfelt) writes:

> On Tue, 4 Apr 2000 22:39:20 -0700 (PDT), lewst wrote:
> >       ^^^^^^^^^^
(...)
> i think something like the following is pretty hard to beat:
> 
> #!/usr/bin/perl
> 
> open(in,$ARGV[0]) || die("can't open $ARGV[0]");
> open(out,">$ARGV[1]) || die("can't open $ARGV[1]);
> 
> while (<in>)
> {
>   unless($i,{$_}++) {push(@uniq,$_);}
> }
> print out (sort(@uniq));

I guess it depends on what "beat" means - I agree with a previous
responder that Perl could probably win a majority of contests based on
terseness, although perhaps doing so is not always of benefit for long
term maintainability and manageability.

For what it's worth, I know these postings are probably untested, but
even after adding some missing quotes in the open lines, I get an
error trying to run the above:

     Can't modify scalar ref constructor in postincrement at
     uniq.pl line 6, near "}++"

I guess I'm not enough of a Perl person to know what to change to
match your original intentions (why would you want to post-increment
the current line read?) - although personally I'd be curious to know
how that particular unless() clause is actually supposed to work.

Here's what I'd probably do in Python - keeping with the above example
semantics of reading the input line by line, but storing all matches
in an in-memory list for sorting prior to output.  Note that I think
normal exception handling can be permitted to take care of file
errors, since Python's exception and traceback is just as helpful and
serves the same end-goal as the Perl "die" clauses.

     #!/usr/bin/env python

     import sys

     input    = open(sys.argv[1])
     output   = open(sys.argv[2],'w')

     uniq     = []
     lastline = None

     while 1:
	 curline = input.readline()
	 if not curline: break

	 if curline != lastline:
	     uniq.append(curline)
	     lastline = curline

     uniq.sort()
     output.writelines(uniq)

Aside from the obvious brevity win of the "while <in>" notation of
Perl versus the readline()/break Pythonism, there's a pretty close
correspondence (noting that the 'if' clause could be placed on a
single line as in the Perl example if desired, but that wouldn't be my
preference).  

Maybe for something this small the maintenance difference is tiny, but
I'd probably rather come back to the Python code 6 or 12 months from
now rather than the Perl variant (particularly if it was part of a
larger system), or perhaps more importantly - would rather hand this
code off to someone else.

But then again, it's not like Perl is satan or something, so different
languages may fit people or circumstances differently.  For my part, I
find myself attracted to items other than brevity (in typing or
syntax) which has drawn me to Python over Perl.

<ASIDE> 
Oh, and BTW, if you don't mind a small nit of mine showing through and
a little constructive criticism - with respect to your comment about
not liking typing, and thus ignoring mixed case.  Please remember that
far more people read a posting than the individual that creates one.
All lowercase text is inherently harder to read and parse visually, so
your small savings in typing time (not having to hold down the Shift
key) makes it harder for readers to read your posting.  The savings on
your part translates into far more waste in energy - at least in
aggregate - on the part of all the readers.  I think it's worth the
time when writing a message to pay attention to such items whenever
possible.
</ASIDE>

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list