Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

TIMTOWTDI effect

by moxliukas (Curate)
on Oct 15, 2002 at 23:12 UTC ( [id://205556]=perlmeditation: print w/replies, xml ) Need Help??

Tonight one of my friends asked for a quick way to filter a stream, which consisted of lines like

wheeeeeeee:CPONKT:shiubi=diubi
He needed to get everything that followed :CPONKT: and print it. Standard Perl stuff.

So me and my other two friends quickly made a brainstorming session (this was happening on IRC) and we came up with the following (untested) solutions:

  1. perl -e 'while(<STDIN>){/:CPONKT:(.*)/; print $1;}'
  2. perl -en 'print {split /:CPONKT:/}[1]'
  3. perl -nep '/:CPONKT:(.*)/;$_=$1;'
  4. perl -ne "/:CPONKT:(.*)/ && print $1;"
  5. perl -nep 's/:CPONKT:(.*)/$1/;'
  6. perl -pne "/:CPONKT:/;$_=$'";

Of course, some of these solutions look a bit fishy (like the split one), but I was really amazed about the TIMTOWTDI effect. It is the first time that I see this so explicitly. It also shows that different programmers think differently, which is also pretty unusual for me, as I come from PHP background. Sure, I've heard of TIMTOWTDI before, but today it has opened my eyes ;)

How would you solve the problem? Are there still more ways of solving this particular task?

Replies are listed 'Best First'.
Re: TIMTOWTDI effect
by blakem (Monsignor) on Oct 15, 2002 at 23:23 UTC
    How about:
    % perl -pe 'substr($_,0,index($_,":CPONKT:")+8,"")'
    For an extreme case of TMTOWTDI check out numerous ways the paris mongers have found to write $A++

    -Blake

Re: TIMTOWTDI effect
by Anonymous Monk on Oct 15, 2002 at 23:53 UTC
    Why -n and -p together? -p does both. As for me, I would solve it by perl -pe s/.*:CPONKT://
Re: TIMTOWTDI effect
by PodMaster (Abbot) on Oct 16, 2002 at 08:17 UTC
    Just because I still don't have -p comitted to memory, and the -n solution was done ;)
    perl -e"print /:CPONKT:(.*)/ while <>" foo > bar
    or if we're golfing ;)
    perl -e"print /:CPONKT:(.*)/ for <>" foo > bar

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: TIMTOWTDI effect
by brx (Pilgrim) on Oct 16, 2002 at 17:11 UTC
    Solution 5 (perl -nep 's/:CPONKT:(.*)/$1/;') is wrong.
    With the same idea you could write :
    perl -pe 's/.*:CPONKT:(.*)/$1/;' # or perl -pe 's/.*?:CPONKT:(.*)/$1/;'# first one if ':CPONKT:' # is seen several times

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://205556]
Approved by hossman
Front-paged by jarich
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-29 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found