#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Build all possible strings based on the following three rules: # 1. Start with 'foobar' # 2. Optionally include first set: # a. zero or one 'a'; # b. zero or one 'b'; and # c. zero or one 'c'. # 3. Optionally include second set: # a. 'X' # b. zero or one 'a'; # c. zero or one 'b'; and # d. zero or one 'c'. my ( @w, %solutions ); { @w = ('foobar'); for my $firstSet ( 0 .. 1 ) { for my $a1 ( 0 .. 1 ) { for my $b1 ( 0 .. 1 ) { for my $c1 ( 0 .. 1 ) { for my $secondSet ( 0 .. 1 ) { for my $a2 ( 0 .. 1 ) { for my $b2 ( 0 .. 1 ) { for my $c2 ( 0 .. 1 ) { if ($firstSet) { if ($a1) { addThisChar('a'); } if ($b1) { addThisChar('b'); } if ($c1) { addThisChar('c'); } } if ($secondSet) { addThisChar('X'); if ($a2) { addThisChar('a'); } if ($b2) { addThisChar('b'); } if ($c2) { addThisChar('c'); } } saveThatPattern(); } } } } } } } } for my $key ( sort { $solutions{$b} <=> $solutions{$a} } sort keys %solutions ) { print " $key => $solutions{ $key }\n"; } } sub addThisChar { push( @w, shift ); } sub saveThatPattern { $solutions{ join( '', @w ) }++; @w = ('foobar'); }