Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked


by moxliukas (Curate)
on Oct 15, 2002 at 23:12 UTC ( #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

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++


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?

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
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-05-25 02:49 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (84 votes). Check out past polls.