Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this messageIn article <f8860640.0411232153.239861d8@posting.google.com>,
William James <w_a_x_man@yahoo.com> wrote:
...
>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.
They don't need out help to make Perl look hideous. It comes built-in.
Post Follow-up to this messageWilliam James wrote: > 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] > } > ----- That awk script does NOT do the same thing as the perl script. The point of the perl script is to modify the file "test.log" without using an intermediate temporary file, producing useful error messages if the file can't be opened for reading or writing. The equivalent awk code would have a very similair structure to the perl code and may not be able to produce such specific, useful diagnostics. > I suggest that awkers that read this should regularly visit that site > and sieze every opportunity to make Perl code look hideous. That seems a bit pointless. I never use it myself, but perl appears to be fairly useful. Ed.
Post Follow-up to this messageEd Morton <morton@lsupcaemnt.com> wrote
> That awk script does NOT do the same thing as the perl script. The point
> of the perl script is to modify the file "test.log" without using an
> intermediate temporary file, producing useful error messages if the file
> can't be opened for reading or writing.
# Modifies the log file without producing an intermediate temporary file.
{ a[$0]++ }
END {
for ( k in a )
print k "|" a[k] >FILENAME
if (close(FILENAME)) print FILENAME,"wasn't open."
}
When I ran this with a nonexistant file, I got this useful message:
awk95: can't open file datafoo
source line number 8
When I tried it on a read-only file, this was the message:
awk95: can't open file data
input record number 4, file data
source line number 5
It would be better if we were told whether the file was being opened for
reading or writing, but a person familiar with Awk should be able to
deduce that.
Anyway, I think that there a lot of Perl users who don't realize how much
can be done with Awk and who would prefer to use a simpler, more elegant
language when the cryptic complexity of Perl isn't needed.
Post Follow-up to this message
William James wrote:
> Ed Morton <morton@lsupcaemnt.com> wrote
>
>
>
>
> # Modifies the log file without producing an intermediate temporary file.
> { a[$0]++ }
> END {
> for ( k in a )
> print k "|" a[k] >FILENAME
> if (close(FILENAME)) print FILENAME,"wasn't open."
> }
awk '...' file > file
may SOMETIMES work too, but I wouldn't count on it and neither would I
count on being able to write to the same file that's still open for
reading as above.
> When I ran this with a nonexistant file, I got this useful message:
>
> awk95: can't open file datafoo
> source line number 8
>
> When I tried it on a read-only file, this was the message:
>
> awk95: can't open file data
> input record number 4, file data
> source line number 5
Just curious - what was the message when you ran it on a file you could
read from but couldn't write to?
> It would be better if we were told whether the file was being opened for
> reading or writing, but a person familiar with Awk should be able to
> deduce that.
Well, yeah, but you could say the same for the perl script - I'm sure it
could've been written to be less expressive too.
> Anyway, I think that there a lot of Perl users who don't realize how much
> can be done with Awk and who would prefer to use a simpler, more elegant
> language when the cryptic complexity of Perl isn't needed.
Maybe so, but I hardly think that's a reason to stalk a perl newgroup
looking for opportunities to cruiticise it. In any case, I'm absolutely
certain that if we were having this conversation in comp.unix.shell one
of the resident perl experts would be able to demonstrate as simple a
script as the awk one - when I see perl solutions posted there to
various problems, they're rarely significantly different from the awk
equivalent.
Ed.
Post Follow-up to this messageEd Morton wrote:
>
>
> William James wrote:
>
>
>
> awk '...' file > file
>
> may SOMETIMES work too, but I wouldn't count on it and neither would I
> count on being able to write to the same file that's still open for
> reading as above.
It will never work on UNIX since the shell will truncate and open file
before executing the command.
-Ed
--
(You can't go wrong with psycho-rats.) (er258)(@)(eng.cam)(.ac.uk)
/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage
Post Follow-up to this messageIn article <41A5F118.8050204@my.sig>, E. Rosten <look@my.sig> wrote: >Ed Morton wrote: This technique is reasonably safe (meaning: I use it regularly and have never had a problem) - at least under Unix. (Of course) It might not work on trash OSs like Novell or Microsoft. > >It will never work on UNIX since the shell will truncate and open file >before executing the command. True. Believe it or not, I've seen people try things like: awk '...' file | (sleep 5;cat > file) (Never tried it myself - don't have any desire to)
Post Follow-up to this messageEd Morton wrote:
>I never use it myself, but perl appears to
>be fairly useful.
Of course. Anything that gigantic and malformed had better be useful.
I'm not saying that it doesn't function effectively.
>William James wrote:
>
>awk '...' file > file
>
>may SOMETIMES work too, but I wouldn't count on it and neither would I
awk '...' file > file does NOT work in a dos-box under windoze 2000
and, as two others have pointed out, it will NEVER work under Unix.
>count on being able to write to the same file that's still open for
>reading as above.
FILENAME is NOT still open for reading; it has been COMPLETELY
read and has been CLOSED. If you feel insecure about it, use
END{ close(FILENAME) ... }.
I, like Kenny McCormack, use this method REGULARLY and have NEVER had
the SLIGHTEST problem with it.
>but I hardly think that's a reason to stalk a perl newgroup
What? "Stalk"? What an extremely pejorative and unfair way of
describing what I proposed. A stalker is someone who, it is feared, may
mug, rape, or murder the stalkee.
>looking for opportunities to cruiticise it.
When did I use the word CRUITICISE or even CRITICIZE? As I said, the
rebel awker merely posted awk code that made the Perl code look ugly.
Simply JUXTAPOSING Awk and Perl is damaging to Perl's reputation.
Many fair-minded people have been fooled by Perl hierophants into
thinking that they have no choice but to put up with obfuscated
ugliness. If we don't show them what Awk can do, no one will. (Besides,
it's fun and gives you a chance to hone your programming skills.)
Post Follow-up to this messageWilliam James wrote: > Ed Morton wrote: <snip> > What? "Stalk"? What an extremely pejorative and unfair way of > describing what I proposed. A stalker is someone who, it is feared, may > mug, rape, or murder the stalkee. From dictionary.com: stalk .. 1. To pursue by tracking stealthily. 2. To follow or observe (a person) persistently, especially out of obsession or derangement. 3. To go through (an area) in pursuit of prey or quarry. What you suggested in your quote below IS stalking, it just doesn't always have the connotations you ascribe to it. > > > > When did I use the word CRUITICISE or even CRITICIZE? Your quote: "I suggest that awkers that read this should regularly visit that site and sieze every opportunity to make Perl code look hideous." You do not need to use the word criticise to express the intent to criticise. As I said, the > rebel awker merely posted awk code that made the Perl code look ugly. Because it did not do the same thing as the perl code and neither does the awk code you posted. The perl code could have been written as simply as the awk code. > Simply JUXTAPOSING Awk and Perl is damaging to Perl's reputation. > > Many fair-minded people have been fooled by Perl hierophants into > thinking that they have no choice but to put up with obfuscated > ugliness. From what I've seen or perl, I prefer awks syntax myself, but I don't think it's up to me to decide what others should consider ugly or beautiful. I'd find it annoying if in this awk newgroup somone started posting perl solutions to every problem just because THEY think we need to be educated on the wonders of perl. If we don't show them what Awk can do, no one will. (Besides, > it's fun and gives you a chance to hone your programming skills.) Is that what you're doing? Saving the perl programmers from themselves? I'd like to just say "go for it", but the danger here is that since there isn't a large awk community watching the perl newsgroup, what will get posted there is half-baked attempts at solving the problem in awk (as in the particular case you pointed out) which will lead perl programmers to think awk is NOT as useful as perl which will have exactly the opposite effect of what you claim to be pursuing. Ed.
Post Follow-up to this messageKenny McCormack wrote: > In article <41A5F118.8050204@my.sig>, E. Rosten <look@my.sig> wrote: > <snip> > > > True. Believe it or not, I've seen people try things like: > > awk '...' file | (sleep 5;cat > file) > > (Never tried it myself - don't have any desire to) > The example I really intended to give was: cat file | awk '...' > file which apparently does work sometimes (according to related questions I've seen posted in comp.unix.shell). Ed.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.