Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here's one way to compute the combinations that can be created by drawing a single element from each of a list of given sets:
#!/usr/bin/perl -l use warnings; use strict; use List::Util qw( reduce ); sub combinations { no warnings qw( once ); reduce { outer_r($a,$b) } [[]], reverse @_; } sub outer_r { my ($ys, $xs) = @_; my @product; foreach my $x (@$xs) { foreach my $y (@$ys) { push @product, [$x, @$y]; } } return \@product; }
The combinations function takes a list of sets (each represented as an arrayref) and returns the combinations that can be created from them:
use Data::Dumper; $Data::Dumper::Terse = 1; $Data::Dumper::Indent = 0; print Dumper( combinations( [1..3], ["a","b"] ) ), "\n"; # [[1,'a'],[1,'b'],[2,'a'],[2,'b'],[3,'a'],[3,'b']]
With this function, we can turn to your question of how best to represent your character sets. I would just use strings to keep things simple. A helper function will convert strings into the form needed by combinations and then convert the results back into strings:
sub charset_combinations { my @charsets = map [split//], @_; map join("", @$_), @{ combinations( @charsets ) }; }
Let's use our new helper to find all of the 3-character combinations that can be made from the charset "abc":
my @abcees3 = charset_combinations( ("abc") x 3 ); print "@abcees3\n"; # aaa aab aac aba abb abc aca acb acc\ # baa bab bac bba bbb bbc bca bcb bcc\ # caa cab cac cba cbb cbc cca ccb ccc
We can even draw successive characters from different character sets:
my @charsets = qw( abc 123 !@$ ); foreach my $string_length (0 .. @charsets) { my @genstrings = charset_combinations( @charsets[0..$string_length-1] ); print "$string_length: @genstrings\n"; } # 0: # 1: a b c # 2: a1 a2 a3 b1 b2 b3 c1 c2 c3 # 3: a1! a1@ a1$ a2! a2@ a2$ a3! a3@ a3$\ # b1! b1@ b1$ b2! b2@ b2$ b3! b3@ b3$\ # c1! c1@ c1$ c2! c2@ c2$ c3! c3@ c3$
I hope this gives you some helpful ideas.

Cheers,
Tom


In reply to Re: character generator by tmoertel
in thread character generator by semio

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 about the Monastery: (3)
As of 2024-03-28 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found