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!


In reply to Working with Binary Numbers by shoness

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.