Following a link that recently came up on the golf mailing list, I ran across this problem, which I thought was ripe for golfing. I thought their input format was ugly, so instead of taking the words to be ignored on standard input, they are passed as command-line arguments (this also complicates the golfing a bit). An example of proper behavior is:
% perl kwic.pl a an the an ostrich a big bird the an bird ^D a BIG bird a big BIRD the an BIRD an OSTRICH %
My best so far is an 85:
$i{$x}=1while$x=pop;s/\w+/$i{$&}or$k{$&}.="$`\U$&\E$'"/egwhile<>;print +@k{sort keys%k}
Then I thought "as an excercise, why not do this in Ruby?" Which yielded this 142:
p={} i={} ARGV.each{|x|i[x]=1}.clear while l=gets;l.gsub(/\S+/){|s|p[s]=''if !p[s];p[s]<<$`+$&.upcase+$'if +!i[s]}end p.sort.each{|x,y|print y}
/s

Update:MeowChow has pointed out Key Word In Context system (golf, anyone)?, which is suspiciously similar, though not identical.


In reply to (golf + ruby-golf) keyword in context by educated_foo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.