in reply to String Replace - Permutation, Combination?
Update: Thought I had x,y and a,b reversed in previous code. Note the order is different than the op's post, but the combinations are the same.#!/usr/bin/perl -w use strict; print "$_\n" for (<{f}{r,x,y}{e,a,b}{d}>); __END__ program: op's post fred fred frad fxed frbd fyed fxed frad fxad frbd fxbd fxad fyed fxbd fyad fyad fybd fybd
Continuing on.. a straightforward implementation of a sub to generate the glob string is shown below.. took a couple of more steps than I thought it would.
#!/usr/bin/perl -w use strict; my $string = 'fred'; my %substitution = ( 2 => 'xy', 3 => 'ab' ); my @variants = gen_variants($string, %substitution); print join("\n",@variants),"\n"; sub gen_variants { my ($string, %substitution) = @_; my @letters = split(//,$string); foreach my $pos (keys %substitution) { my @newltrs = split(//,$substitution{$pos}); $letters[$pos-1] = join(',',$letters[$pos-1],@newltrs); } @letters = map{"{$_}"}@letters; my $globstr = join('',@letters); print "glob string = $globstr\n"; return (glob($globstr)); } __END__ prints: glob string = {f}{r,x,y}{e,a,b}{d} fred frad frbd fxed fxad fxbd fyed fyad fybd
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: String Replace - Permutation, Combination?
by ikegami (Patriarch) on Dec 15, 2010 at 02:23 UTC | |
|
Re^2: String Replace - Permutation, Combination?
by cbdoc (Novice) on Dec 14, 2010 at 22:34 UTC |