I've a set of binary (base 2) numbers, where the '-' means BOTH 0 and 1 are to be substituted:
Hence the above array reads:my @data = qw( 000- 0101 011- 1-0- );
Now the last part of my problem is easy, since I can change binary to decimal like this:my @data = qw( 0 1 5 6 7 8 9 12 13 );
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: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; }
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....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; } }
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!
In reply to Working with Binary Numbers by shoness
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |