http://qs321.pair.com?node_id=640813

shoness has asked for the wisdom of the Perl Monks concerning the following question:

The novice monk asked his teachers this kōan...

I've a set of binary (base 2) numbers, where the '-' means BOTH 0 and 1 are to be substituted:

my @data = qw( 000- 0101 011- 1-0- );
Hence the above array reads:
my @data = qw( 0 1 5 6 7 8 9 12 13 );
Now the last part of my problem is easy, since I can change binary to decimal like this:
sub to_binary { my $str = shift; my $value = 0; for (my $ii=0; $ii<length($str); $ii++) { $value = 2 * $value + substr($str, $ii, 1); } return $value; }
The middle part, expanding the array members that contain '-' should probably be done using a recursive subroutine call since there can be multiple dashes. This is where I'm working now:
for (my $ii=0; $ii<@data; $ii++) { if (@data[$ii] =~ /-/) { splice(@data, $ii, 1, bits(@data[$ii])); } } sub bits { my $str = shift; if ($str =~ /-/) { if (substr($str, $ii, 1) eq '-') { bits( substr($str, $ii, 1, '0' ); bits( substr($str, $ii, 1, '1' ); # somehow don't return anything... ???? } } else { return $str; } }
My approach to the binary conversion is brute-force. My approach to solve the "-" expansion is going to have to get more brute-force. I'm sure that I'm missing something on both accounts....

I looked into it, but I don't think Set::Scalar is useful here. I also don't see any proper binary number modules to start with up on CPAN either.

Your help is appreciated.

As always,
Thanks!