[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] [olug] Bash TCP scripting



On Dec 31, 2007, at 7:24 PM, George Neill wrote:
OO PERL :)  Just curious ... what was the assignment?

Given a text file, do a case-insensitive word count. Print each word and the number of times it appeared in the text file.

The Perl solution I wrote in class while the teacher was explaining the assignment:


while (<>) {
   foreach (/([a-zA-Z0-9'-]+)/g) {
      $cnt{lc($_)}++;
   }
}

foreach (sort keys %cnt) {
   print "$_ $cnt{$_}\n";
}


2 weeks later I turned in my 400 lines of C++ (which I can't share or the nuns will whack my knuckles with a ruler).

As I mentioned, the C++ solution was a lot more code then just solving the problem. In C++ we set up a List object which was a "linked list" of Node objects and each Node object contained a Word object. Each object has it's own methods, and we set up operator overloading to dump objects into ostreams (STDOUT), etc.

$ wc -l *
     10 input.txt
    137 list.cpp
     31 list.h
     28 main.cpp
     22 Makefile
    149 word.cpp
     36 word.h
    413 total

That said, my Perl program kicked out the exact same output and only took 15 minutes to write and debug. :) My teacher was not surprised. Like I mentioned, solving the problem as specified apparently wasn't the point. If it were then all CS classes would let you turn in a solution in any language you want? (Some upper level classes do, apparently?) IANAE. (I am not an educator. -grin-)

There are many things you would never write in Perl. Embedded systems, graphics card drivers, all kinds of low-level stuff. I have no interest in ever coding those systems, but I should be better of for getting a little C++ under my belt? I am interested in scientific programming (bioinformatics), parts of which are insanely computational, so I'll probably end up in C land for that stuff one of these days...? A typical solution is Perl (or Java) doing all the glue, user & system interfacing, and data transformations while insanely optimized C programs sit in the back doing TeraFLOPS of hard- core processing.

Toodles,

j
loves to watch himself type, apparently  :)