Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello choroba,

I propose a non concurring idea perhaps interesting, infact my result is longer and the resulting glob will produce too much occurences (57600 instead of 24 ;). Maybe it can be used and reduced..

> Find the shortest glob expression generating the words in the list.

..words in the list and only them!

> ..insults?

brainy!! :)

use warnings; use strict; use Data::Dump; sub list2glob { my $table; my $table_len; foreach my $word ( sort {length $b <=> length $a} @_ ){ $table_len = length $word unless $table_len; push @$table, [ map{ length $word > $_ ? substr( $word,$_,1 ) +: '' }0..$table_len-1 ]; } # dd $table; my $glob; foreach my $col (0..$table_len-1){ print "\nColumn $col\n"; my %possible; foreach my $row ( @$table ){ $possible{ $row->[$col] }++; } print "occurences: ";dd %possible; if ( 1 == scalar keys %possible ){ $glob .= join'',keys %possible; } else { $glob .= '{'.( join ',', keys %possible ).'}'; } print "actual glob expression: $glob\n"; } my @all = glob $glob; print "\n\nRESULT: glob $glob produces ",scalar @all," elements\n" +; } list2glob(qw( abdel abdelmn abdelmo abdfgkl abdfgklmn abdfgklmo abdfghkl abdfghklmn abdfghklmo abdfgijkl abdfgijklmn abdfgijklmo acdel acdelmn acdelmo acdfgkl acdfgklmn acdfgklmo acdfghkl acdfghklmn acdfghklmo acdfgijkl acdfgijklmn __END__ Column 0 occurences: ("a", 24) actual glob expression: a Column 1 occurences: ("b", 12, "c", 12) actual glob expression: a{b,c} Column 2 occurences: ("d", 24) actual glob expression: a{b,c}d Column 3 occurences: ("e", 6, "f", 18) actual glob expression: a{b,c}d{e,f} Column 4 occurences: ("l", 6, "g", 18) actual glob expression: a{b,c}d{e,f}{l,g} Column 5 occurences: ("", 2, "k", 6, "m", 4, "h", 6, "i", 6) actual glob expression: a{b,c}d{e,f}{l,g}{,k,m,h,i} Column 6 occurences: ("l", 6, "k", 6, "o", 2, "n", 2, "j", 6, "", 2) actual glob expression: a{b,c}d{e,f}{l,g}{,k,m,h,i}{l,k,o,n,j,} Column 7 occurences: ("m", 4, "l", 6, "k", 6, "", 8) actual glob expression: a{b,c}d{e,f}{l,g}{,k,m,h,i}{l,k,o,n,j,}{m,l,k, +} Column 8 occurences: ("", 10, "n", 2, "m", 4, "o", 2, "l", 6) actual glob expression: a{b,c}d{e,f}{l,g}{,k,m,h,i}{l,k,o,n,j,}{m,l,k, +}{,n,m,o,l} Column 9 occurences: ("o", 2, "n", 2, "m", 4, "", 16) actual glob expression: a{b,c}d{e,f}{l,g}{,k,m,h,i}{l,k,o,n,j,}{m,l,k, +}{,n,m,o,l}{o,n,m,} Column 10 occurences: ("", 20, "n", 2, "o", 2) actual glob expression: a{b,c}d{e,f}{l,g}{,k,m,h,i}{l,k,o,n,j,}{m,l,k, +}{,n,m,o,l}{o,n,m,}{,n,o} RESULT: glob a{b,c}d{e,f}{l,g}{,k,m,h,i}{l,k,o,n,j,}{m,l,k,}{,n,m,o,l} +{o,n,m,}{,n,o} produces 57600 elements

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.

In reply to Re: Challenge: Generate a glob patterns from a word list by Discipulus
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found