Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: character generator

by tmoertel (Chaplain)
on Oct 25, 2004 at 20:00 UTC ( #402295=note: print w/ replies, xml ) Need Help??

in reply to character generator

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.


Comment on Re: character generator
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2014-11-27 12:09 GMT
Find Nodes?
    Voting Booth?

    My preferred Perl binaries come from:

    Results (184 votes), past polls