Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Working with Binary Numbers

by hobbs (Monk)
on Sep 27, 2007 at 04:39 UTC ( [id://641274]=note: print w/replies, xml ) Need Help??


in reply to Working with Binary Numbers

Just for fun, the first solution that came into my head. It's not the most efficient (I agree with the use of glob as a practical solution) but I think it's reasonably simple to understand. No recursion, only iteration.
use strict; my @data = qw( 00- 0101 011- 1-0- ); while (grep /-/, @data) { @data = map do { unless (/-/) { $_; } else { my ($zero, $one); ($zero = $_) =~ s/-/0/; ($one = $_) =~ s/-/1/; ($zero, $one); } }, @data; } print join(" ", map oct "0b$_", @data), "\n";
Note that the grep can actually be removed if you don't mind having a 'map' with side-effects; you can keep track of whether you did any replacements as you go along, and stop after the first time that there weren't any.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://641274]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found