For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2004 > Re: Pick on Perl - in Python









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Pick on Perl - in Python
Paddy McCarthy

2004-11-29, 3:58 pm

w_a_x_man@yahoo.com (William James) wrote in message news:<f8860640.0411232153.239861d8@posting.google.com>...
> I noticed something amusing at the Perl site
> http://www.tek-tips.com/viewthread....d=955815&page=1
>
> To convert a file like this
>
> header.jpg
> header.jpg
> footer.gif
> wrap.bmp
>
> to
>
> wrap.bmp|1
> header.jpg|2
> footer.gif|1
>
> the most highly decorated programmer at the site posted this:
>
> -----
> my %log;
> open(LOG, "<test.log") || die qq(Can't open "test.log" for input!\n);
> chomp, $log{$_}++ while <LOG>;
> close(LOG) || die qq(Can't close "test.log"!\n);
> open(LOG, ">test.log") || die qq(Can't open "test.log" for output!\n);
> writelog(%log};
> close(LOG) || die qq(Can't close "test.log"!\n);
>
> sub writelog {
> my %log = @_;
> while (my ($k, $v) = each %log) {
> print LOG join("|", ($k, $v)), "\n";
> }
> }
> -----
>
> Then a rebel awker posted this:
>
> -----
> { a[$0]++ }
> END { for ( k in a )
> print k "|" a[k]
> }
> -----
>
> I suggest that awkers that read this should regularly visit that site
> and sieze every opportunity to make Perl code look hideous.


Hi fellow AWK folks,
I couldn't help thinking that the above was unfair ... for about a micro-
second.
But I then got thinking about how I would tackle this in Python.

Whoops, I should first explain that I was weaned on AWK, am forced to write
in Perl at work (Python forbidden, AWK tolerated); and write in Python and
AWK, for leisure.

My Python solution follows:


### START FILE ###

''' count_uniq.py Counts the unique lines in a file.

Rather like `sort myfile|uniq -c` on unix,
(except the count is to the right).

Example:
pad@Yes-man:/tmp$ cat tmp.log
header.jpg
header.jpg
footer.gif
wrap.bmp
pad@Yes-man:/tmp$ python count_uniq.py tmp.log
footer.gif 1
header.jpg 2
wrap.bmp 1
pad@Yes-man:/tmp$
'''
import fileinput

# get all lines and strip the newline
lines = [ line.rstrip() for line in fileinput.input() ]
# use a set to generate unique lines then count
unique = [ name +" "+ str(lines.count(name)) for name in sorted(set(lines)) ]
print "\n".join(unique)

### END FILE ###

Paddy.
William James

2004-11-29, 3:58 pm

In Ruby:

---------
print "foo"; print "bar\n"

"foo".display
puts "bar"

3.times {print "foo"; print "bar"}

printf("\nThe value of %s is %d\n", "foobar", 99)
---------

produces

foobar
foobar
foobarfoobarfoobar
The value of foobar is 99
William James

2004-12-02, 3:56 pm

Python does seem preferable to Perl. However...

Some time ago, after I looked at a couple of tutorials, the first
two minutes spent toying with the language showed that it couldn't do
an absurdly simple thing.

Print "foobar\n" with 2 print commands.

In Awk:
printf "foo"; printf "bar\n"
or
ORS=""
print "foo"; print "bar\n"

This utterly trivial task is beyond Python's "print" command
(unless you fiddle with the inner mechanism).

print "foo",
print "bar"

produces "foo bar". There is no way to prevent "print" from
appending a newline or prepending a space (if it follows a print
statement terminated with ","). The implementors of the language
decided that since they didn't like to print without a newline
or space between strings, you shouldn't be allowed to do it.

Did I say "no way"? Change that to "no sane, simple way".

import sys
print "foo",
sys.stdout.softspace=0
print "bar"

This is really fun! I get to type a gigantic compound word just to
print a string! And I get to type "sys.stdout.softspace=0" every time,
yes, every single time I want to suppress the space!

Since the high and mighty implementors frown on us peons being able
to use a simple and fully functional "print", this is the standard way:

import sys
sys.stdout.write("foo")
print "bar"

That the following statement isn't obvious to most people shows that
that the majority have been conditioned to slavish obedience:
In any scripting language one should be able to print "foobar" in chunks
without having to import anything and without having to use a complex
construction such as sys.stdout.write().
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com