Corion has asked for the wisdom of the Perl Monks concerning the following question:

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)

Replies are listed 'Best First'.
Re: Golf: Fuzzy finder / autocomplete
by Anonymous Monk on Jun 22, 2015 at 19:52 UTC

    First pass.

    golf tip - when sorting by small non-negative numbers, don't sort :)

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1131533 @ARGV=qw(mig main_generator migrations django_migrations django_admin_log myfoo\bar.txt ); # 1 2 3 4 5 6 7 + 8 #234567890123456789012345678901234567890123456789012345678901234567890 +1234567890 #-- count from here $p?/$p/&&($a["@+"+9*length$&].=$_.$/):($p=s/\B/.*?/gr)for@ARGV;print@a
Re: noGolf: Fuzzy finder / autocomplete
by Discipulus (Canon) on Jun 23, 2015 at 08:50 UTC
    ah! i saw this last night and i started mumbling around a lot of permutations of regexes.. but my brain was too little to afford it.
    Then, before sleeping, a simpler solution (regex based) come to my mind.
    I'm not a Perl golfer at all and i cant even read the proposed solution: so i answer to the challenge part of the question. Anyway my Perl tends to be unreadable, so my simple solution is quite obfu..

    But, in the example you gave using the 'mig' pattern, Corion, the resulting ranking of choices ( migrations django_migrations main_generator django_admin_log ) is equal to the last one in the python example, but i dont understand why 'django_migrations' comes before 'main_generator': maybe i'm wrong but in my mind 'shortest leftmost' means 'leftmost shortest' and this is the normal way to order strings..

    Here is my nine lines (of relevant code) solution, using plain Perl (i'm sure it can be shortened and golfed a lot..). It handles also the correct order of similar words:
    #!perl use strict; use warnings; @ARGV=qw(mig main_generator migrations django_migrations django_admin_ +log myfoo\bar.txt main_generatorZ XXX xxx); my $patt = shift; my $rx = (map {qr/$_/} join '', (map { '([^'.$_.']*)(?:'.$_.')'} $patt +=~/./g ))[0] ; my %r; map { $_=~/$rx/ ? push @{$r{join'_', map{sprintf "%02d",length $_} $_=~/$rx/g}},$_ : push @{$r{join'_', map{'99'} 1..length $patt}} ,$_ }@ARGV; print map { join ' ', sort @{$r{$_}},"\n" } sort keys %r; __OUT__ migrations main_generator main_generatorZ django_migrations django_admin_log XXX myfoo\bar.txt xxx

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Golf: Fuzzy finder / autocomplete
by choroba (Cardinal) on Jun 23, 2015 at 12:08 UTC
    Schwartzian transform doesn't seem good for golfing. I tried not to use regexes:
    #!/usr/bin/perl -l @ARGV=qw(mig main_generator migrations django_migrations django_admin_ +log myfoo\bar.txt ); @c=split//,shift;print for map$_->[0],grep$_->[2]>0,sort{$a->[2]<=>$b- +>[2]or$a->[1]<=>$b->[1]} map{my$p;$s=$_;my@d;$p>=0 and push@d,$p=index$s,$_,$p for@c;[$s,$d[0], +$d[-1]-$d[0]]}@ARGV

    Update: Fixed to handle xmxgx: index accepts negative positions and treats them as 0.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ