every combination of Y/N in 5 positions
- From: joemacbusiness@xxxxxxxxx
- Date: Mon, 31 Mar 2008 13:57:11 -0700 (PDT)
Hi All,
This has probably already been written but I did not see it on CPAN.
Is there a code snippent that can print every possible combination
of Y/N's in a 5 position array or string?
For example: Y Y Y Y Y becomes
N Y Y Y Y
Y N Y Y Y....
Y Y N N Y etc.
Here is my first attempt but it only handles a single field change
moving from left to right etc...
Thanks, --Joe Mac.
#!/usr/bin/perl
my @array = qw(Y Y Y Y Y);
&jumble(0);
&jumble(1);
&jumble(2);
&jumble(3);
&jumble(4);
sub jumble {
my $arg = shift;
# print "$arg\n";
$newvar = &changeIt($arg);
if ($arg == 0){
print "$newvar $array[1] $array[2] $array[3] $array[4]\n";
} elsif ($arg == 1){
print "$array[0] $newvar $array[2] $array[3] $array[4]\n";
} elsif ($arg == 2){
print "$array[0] $array[1] $newvar $array[3] $array[4]\n";
} elsif ($arg == 3){
print "$array[0] $array[1] $array[2] $newvar $array[4]\n";
} elsif ($arg == 4){
print "$array[0] $array[1] $array[2] $array[3] $newvar\n";
}
}
sub changeIt {
my $var = shift;
#print "array[$var] is $array[$var]\n";
if ($array[$var] eq "Y"){
$var = "N";
}
return("$var");
}
#
# $ perl ./jumble.pl
N Y Y Y Y
Y N Y Y Y
Y Y N Y Y
Y Y Y N Y
Y Y Y Y N
.
- Follow-Ups:
- Re: every combination of Y/N in 5 positions
- From: smallpond
- Re: every combination of Y/N in 5 positions
- Prev by Date: Re: printf: zero pad after the decimal a given amount
- Next by Date: Re: Windows paths in glob
- Previous by thread: FAQ 8.3 How do I do fancy stuff with the keyboard/screen/mouse?
- Next by thread: Re: every combination of Y/N in 5 positions
- Index(es):
Relevant Pages
|