Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Normalization input file 2

Status
Not open for further replies.

Rieekan

Programmer
Apr 2, 2001
737
US
Hey guys,

I'm sure that I am over complicating this problem too much while trying to find a solution for it, but regardless, I'm at a loss as to where to go.

I have a task to create a script to dynamically generate all of the possible scenarios for a set of information. The information I'm looking for is a set of zeros and ones (basically on/off flags) in all possible combinations for a variable set of options.

While this is just the part I'm having difficulty with, I need to be able to have a user enter a number (any number should be accepted) and then store the possible combinations into a manner that can be easily manipulated later.

For instance, if a user enters 3, the script should store the values:
Code:
1,0,0
1,1,0
1,0,1
1,1,1
0,1,0
0,1,1
0,0,1
0,0,0

While I'm sure this should be fairly straight forward, I'm at a loss as to how to get the coding around this. Any help would be appreciated.

- Rieekan
 
user enters n
Code:
$max_loop=2^n;
for ($loop=0;$loop<=$max_loop;$loop++) {
  print "binary for $loop";
  chop the string into an array
}
a post of ishnid's gave me the idea
Code:
#!/usr/bin/perl -w
use strict;
[b]use Math::BaseCnv[/b] qw(cnv);

# start at zero
my $str = 0;

my $returned;
do {
   # Math::BaseCnv::cnv returns capital letters, so convert and then lowercase the answer
   $returned = lc( cnv( $str++, 10, 36 ) );

   # print, with zero padding
   printf "%06s\n", $returned if ( $returned =~ /^[a-z]/ );

   # keep going until we hit 'zzzzzz'
} while ( $returned ne 'zzzzzz' );
I'm assuming if that module doesn't do binary it wouldn't so hard to write one

--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
rharsh posted a neat recursive algorithm a couple of weeks back (for genetic mutations). A simplified version would do what you want. But I can't seem to find it... [sad]
 
I modified the code from thread219-1089407 (the same code that stevexff referred to.) $length would be the user's input.

Code:
my $length = 3;
my @chars = qw/0 1/;
my (@temp, @combinations);

sub rotateChars ($) {
    foreach (@chars) {
        $temp[$_[0]] = $_;
        if ($_[0]+1 != $length) {
            rotateChars($_[0]+1);
        } else {
            push @combinations, join('', @temp);
        }
    }
}

rotateChars(0);
print "$_\n" foreach (@combinations);
 
I knew I had seen something similar, but like Steve, I couldn't find it when I needed it.

Thanks guys!

- Rieekan
 
c'rect y'are

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thou art indeedsby correct is all ;-)

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top