Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: Pick on Perl - in Python
w_a_x_man@yahoo.com (William James) wrote in message news:<f8860640.0411232153.239861d8@pos
ting.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.

Report this thread to moderator Post Follow-up to this message
Old Post
Paddy McCarthy
11-29-04 08:58 PM


Re: Pick on Perl - in Python
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

Report this thread to moderator Post Follow-up to this message
Old Post
William James
11-29-04 08:58 PM


Re: Pick on Perl - in Python
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().

Report this thread to moderator Post Follow-up to this message
Old Post
William James
12-02-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:43 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.