Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: character generator

by tmoertel (Chaplain)
on Oct 25, 2004 at 20:00 UTC ( [id://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.

Cheers,
Tom

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-10-09 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (45 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.