glob to the rescue?
glob can generate string combinations/permutations in many cases, depending on your needs.
#!/your/perl/here
use strict;
use warnings;
my %line = ('A' =>1,
'B' =>1,
'C' =>1);
# last combination to emit (based on grep filter)
my $last = 'AYW';
# build up strings for use with glob()
my @line_keys = sort keys %line;
my @tojoin = sort ('', qw (W X Y Z));
my $line_keys = join ',', @line_keys;
my $tojoin = join ',', @tojoin;
# alternation in glob uses '{string1,string2,...}'
my $glob = '{'
. join( '}{',
$line_keys,
$tojoin,
$tojoin )
. '}';
# globbing
@glob = glob $glob;
# only keep strings shorter than $last,
# or le $last
my @glob = grep length($_) < length($last)
|| $_ le $last,
@glob;
# populate hash (easier ways???)
@line{@glob} = (1) x @glob;
# print results (note sort sub)
foreach my $key (sort string_length keys %line)
{
print "$key => $line{$key}\n";
}
# sort by length, then lexicographically
sub string_length
{
length($a) <=> length($b)
or
$a cmp $b
}
yields
A => 1
B => 1
C => 1
AW => 1
AX => 1
AY => 1
AZ => 1
BW => 1
BX => 1
BY => 1
BZ => 1
CW => 1
CX => 1
CY => 1
CZ => 1
AWW => 1
AWX => 1
AWY => 1
AWZ => 1
AXW => 1
AXX => 1
AXY => 1
AXZ => 1
AYW => 1
-QM
--
Quantum Mechanics: The dreams stuff is made of
-
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.