I just stumbled across "Fuzzy finder in 10 lines of Python" and thought to myself that this would make a nice golf exercise, or at least in my implementation, a weird obfuscation. The idea is to sort a set of strings by order of the shortest leftmost occurance of a sequence of characters, excluding any string that doesn't contain the characters in sequence. You can think of this like some autocompletion. For example, for the string mig in the search set main_generator migrations django_migrations django_admin_log , the ranking of choices would be

migrations
django_migrations
main_generator
django_admin_log

My solution, as also taken in the Python post, is to build a (somewhat saner/safer) regex from the input string and then search the input list against that.

The sample harness gets the data in @ARGV. The first argument is the substring to search, the rest is the list to sort. The sorted list is to be printed to STDOUT, one item per line.

When I remove the whitespace, my solution gets to 123 bytes.

#!perl -wl @ARGV=qw(mig main_generator migrations django_migrations django_admin_ +log myfoo\bar.txt ); # 1 2 3 4 5 6 7 + 8 #234567890123456789012345678901234567890123456789012345678901234567890 +12345678901234567890 #-- count from here ($i=shift)=~s#(.)(?!\z)#[$1].*?#g; print substr $_,16 for sort(map{ /($i)/ ? sprintf'%08x%08x%s',$+[0]-$-[0],$-[0],$_ : () }@ARGV)

The oneliner is:

#!perl -wl @ARGV=qw(mig main_generator migrations django_migrations django_admin_ +log myfoo\bar.txt ); # 1 2 3 4 5 6 7 + 8 #234567890123456789012345678901234567890123456789012345678901234567890 +12345678901234567890 #-- count from here ($i=shift)=~s#(.)(?!\z)#[$1].*?#g;print substr $_,16 for sort(map{/($i +)/?sprintf'%08x%08x%s',$+[0]-$-[0],$-[0],$_:()}@ARGV)

In reply to Golf: Fuzzy finder / autocomplete by Corion

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.