Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Challenge: Generate a glob patterns from a word list

by Discipulus (Abbot)
on May 04, 2021 at 07:28 UTC ( #11132009=note: print w/replies, xml ) Need Help??


in reply to Challenge: Generate a glob patterns from a word list

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11132009]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-09-23 00:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?