Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Get me excited about perl

by BrowserUk (Patriarch)
on Sep 19, 2012 at 19:55 UTC ( [id://994514]=note: print w/replies, xml ) Need Help??


in reply to Get me excited about perl

Show them Perl's greatest asset -- concise solutions to everyday problems.

If you have time, pick some task that will resonate with as many of them as possible and get one or more of them to solve the chosen task in their favoured langugages before the day.

By way of example (because the examples already exist): frequency count the words in a text file.

  • C (211 lines):
  • C++ (79 lines):
  • Java (68 lines):
  • Python (24 lines):
    #!/usr/local/bin/python # $Id: wordfreq.python,v 1.9 2001/05/11 17:44:00 doug Exp $ # http://www.bagley.org/~doug/shootout/ # # adapted from Bill Lear's original python word frequency counter # # Joel Rosdahl suggested using translate table to speed up # word splitting. That change alone sped this program up by # at least a factor of 3. # # with further speedups from Mark Baker import sys def main(): count = {} i_r = map(chr, range(256)) trans = [' '] * 256 o_a, o_z = ord('a'), (ord('z')+1) trans[ord('A'):(ord('Z')+1)] = i_r[o_a:o_z] trans[o_a:o_z] = i_r[o_a:o_z] trans = ''.join(trans) rl = sys.stdin.readlines lines = rl(4095) while lines: for line in lines: for word in line.translate(trans).split(): try: count[word] += 1 except KeyError: count[word] = 1 lines = rl(4095) l = zip(count.values(), count.keys()) l.sort() l.reverse() print '\n'.join(["%7s\t%s" % (count, word) for (count, word) in l] +) main()
  • Haskell (20 lines):

And then write & run your perl solution in real time:

perl -nle"y/a-zA-Z/ /cs; ++$h{$_} for split }{ print qq[$_:$h{$_}] for + sort keys %h" theFile break:1 brief:1 bring:3 brought:2 buffalo:16 burden:1 but:20 by:16 call:2 called:6 came:2 campaign:1 can:36 cannot:2 capable:1 capitals:1 career:2 cart:1 case:2 ...

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

Replies are listed 'Best First'.
Re^2: Get me excited about perl
by tobyink (Canon) on Sep 19, 2012 at 20:21 UTC

    php -R 'foreach (str_word_count(strtolower($argn), 1) as $w) $h[$w]++;' -E 'ksort($h); foreach ($h as $w=>$c) print "$w:$c\n";' <theFile

    Update: it's sorted now.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Is the output sorted?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

      tobyink,

      Okay, what's the joke?

      You brought 'php' to a thread on 'Get me excited about perl'!

      I can always use a good laugh...Ed

      "Well done is better than well said." - Benjamin Franklin

        You brought 'php' to a thread on 'Get me excited about perl'!

        Only as a reply to a post which included C, C++, Java, Python and Haskell source code.

        My point is that Perl is not unique in its ability to provide concise solutions to text processing problems. The PHP solution is a little longer, sure, but is arguably more readable than the Perl one (to somebody who knows both languages) due to not needing to rely on idioms like the Eskimo kiss operator.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://994514]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found