in reply to Re: all binary combinations
in thread all binary combinations

mmmm thanks. But i don't really get it. Is there a way to save the solutions in a hash where each combination is a key? (i'm sure there is but i just don't know how :)

Replies are listed 'Best First'.
Re^3: all binary combinations
by FunkyMonk (Bishop) on Sep 04, 2009 at 16:31 UTC
    To understand it, read the man page for glob. Then understand the following:

    print "$_\n" for glob "{0,1}"; # 0 1 print "$_\n" for glob "{0,1}{0,1}"; # 00 01 10 11 print "$_\n" for glob "{0,1}{0,1}{0,1}"; # 000 001 010 011 100 101 110 + 111

    Then read up on the repetition operator in perlop, and understand:

    print "AB" x 1; # AB print "AB" x 2; # ABAB print "AB" x 3; # ABABAB

    To use it to create keys in a hash, use something like

    use Data::Dumper; my %hash = map { $_ => 1 } glob "{0,1}" x 3; print Dumper \%hash; __END__ $VAR1 = { '011' => 1, '010' => 1, '111' => 1, '000' => 1, '101' => 1, '001' => 1, '100' => 1, '110' => 1 };


    Unless I state otherwise, all my code runs with strict and warnings
Re^3: all binary combinations
by ikegami (Patriarch) on Sep 04, 2009 at 16:24 UTC
    Yes. Iterate over the results and use each result as a key.
      I figured that but the thing is that i don't know how and where the results are stored. I read about glob but that confused me more!!
        The results are returned by the bits function as a list. You can iterate over the list using a foreach loop, a hash slice, map, etc.
        my %hash; for my $bits ( bits(3) ) { $hash{$bits} = ...; }
        my %hash; @hash{ bits(3) } = ...;
        my %hash = map { $_ => ... } bits(3);