It's an idea...

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11131998 use warnings; use Algorithm::Diff qw(traverse_sequences); use List::Util qw( first ); use Test::More; sub list2glob { @_ == 2 and return pair(@_); my @best; for my $i ( 0 .. $#_ - 1 ) { for my $j ( $i + 1 .. $#_ ) { my @rest = ( pair(@_[$i, $j]), map $_[$_], grep $_ != $i && $_ != $j, 0 .. $#_ ); my $try = list2glob(@rest); $best[length $try] = $try; } } return first { defined } @best; } sub pair { my $answer = my $left = my $right = ''; my @from = split //, shift; my @to = split //, shift; traverse_sequences( \@from, \@to, { MATCH => sub { length $left . $right and $answer .= "{$left,$right}"; $left = $right = ''; $answer .= $from[shift()] }, DISCARD_A => sub {$left .= $from[shift()]}, DISCARD_B => sub {$right .= $to[pop()]}, } ); length $left . $right and $answer .= "{$left,$right}"; return $answer; } is list2glob('a', 'b'), '{a,b}'; is list2glob('ab', 'ac'), 'a{b,c}'; is list2glob('aXb', 'aYb'), 'a{X,Y}b'; is list2glob(qw( a1b3c a1b4c a2b3c a2b4c )), 'a{1,2}b{3,4}c'; is list2glob(qw( /ab/ef/ij/kl /ab/ef/ij/mn /ab/ef/ij /cd/gh/ij/kl /cd/gh/ij/mn /cd/gh/ij )), '/{ab,cd}/{ef,gh}/ij{/{kl,mn},}'; done_testing();

Outputs:

ok 1 ok 2 ok 3 ok 4 ok 5 1..5

See if you can figure out why I left out some tests :)


In reply to Re: Challenge: Generate a glob patterns from a word list by tybalt89
in thread Challenge: Generate a glob patterns from a word list by choroba

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.