Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Script critique requested

by jhumphreys (Novice)
on Oct 04, 2012 at 17:25 UTC ( #997265=perlquestion: print w/ replies, xml ) Need Help??
jhumphreys has asked for the wisdom of the Perl Monks concerning the following question:

Knowledgeable Monks-

Bearing in mind that I'm a novice, what's your opinion of the following script that requests decimal input (<256) and gives binary output?:

#!/usr/bin/perl #dectobin3.pl use warnings; use strict; print "Enter a decimal number, less than 256, to be converted to a bin +ary number: "; chomp(my $dec = <STDIN>); my $dec_a = $dec & 128; my $dec_b = $dec & 64; my $dec_c = $dec & 32; my $dec_d = $dec & 16; my $dec_e = $dec & 8; my $dec_f = $dec & 4; my $dec_g = $dec & 2; my $dec_h = $dec & 1; print "\n", $dec_a <=> 0, $dec_b <=> 0, $dec_c <=> 0, $dec_d <=> 0, $d +ec_e <=> 0, $dec_f <=> 0, $dec_g <=> 0, $dec_h <=> 0, "\n";

Comment on Script critique requested
Download Code
Re: Script critique requested
by toolic (Chancellor) on Oct 04, 2012 at 17:32 UTC
    I think it can be simplified (subject to your <256 constraint) using printf:
    use warnings; use strict; print "Enter a decimal number, less than 256, to be converted to a bin +ary number: "; chomp(my $dec = <STDIN>); printf "%08b\n", $dec;

    See also the FAQ:

    perldoc -q convert

    But, yours is an interesting use of the <=> operator.

      Toolic, thanks; and agreed, I just haven't learned the printf operator yet. (Slowly working my way through intro text by Cozens.)

      Best,

      J.

Re: Script critique requested
by aitap (Deacon) on Oct 04, 2012 at 17:39 UTC

    Unfortunately, this is alot easier.

    First of all, you could use some loop starting with multiplying a variable by 2, like this:

    my $bit = 256; for (1..8) { $bit /= 2; print $dec & $bit; }

    Secondly, you can just use pack and unpack instead: print unpack ("B8", (pack "c",$dec)); Explanation: pack the number as a signed char value (0..255) in a byse sequence and unpack as 8 bits.

    Sorry if my advice was wrong.

      aitap, thanks for your reply. Am slowly working my way through intro Perl text by Cozens and pack and unpack are coming up. Will study your reply after I learn these.

      Best,

      J.

Re: Script critique requested
by roboticus (Canon) on Oct 04, 2012 at 17:46 UTC

    jhumphreys:

    Well, it works, but I'm not really a fan of the use of the <=> operator, though it's certainly clever.</opinion>

    Also, you're brute forcing things by doing the repetitive work yourself instead of having the computer do it. For example, you could do it like:

    #!/usr/bin/perl #dectobin3.pl use warnings; use strict; print "Enter a decimal number, less than 256, to be converted to a bin +ary number: "; chomp(my $dec = <STDIN>); my @bits = map { ($dec & 1<<$_) <=> 0 } reverse 0 .. 7; print "Bits=",join("", @bits), "\n";

    That way, you can use it for larger numbers without the tedious copy, paste, edit cycle. Generally, whenever you start typing something like $bit_1, $bit_2, etc., it's time to consider using an array.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      roboticus, thanks for the reply--very useful! Am slowly working my way through intro Perl text by Cozens, so haven't learned map or join yet. But I like the array and the use of reverse you recommend.

      Best,

      J.

        jhumphreys:

        Sorry, I was a bit too idiomatic there. I probably should've suggested something more like:

        my @bits; for (128 64 32 16 8 4 2 1) { push @bits, ($dec & $_) <=> 0; }

        Basically, I saw the wall of:

        my $dec_a = $dec & 128; my $dec_b = $dec & 64; my $dec_c = $dec & 32; my $dec_d = $dec & 16; my $dec_e = $dec & 8; my $dec_f = $dec & 4; my $dec_g = $dec & 2; my $dec_h = $dec & 1;

        and thought to myself that really a loop was needed.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Script critique requested
by Anonymous Monk on Oct 05, 2012 at 01:48 UTC

    Hi,

    Others have given you good advice on how to do it.

    My two Ps worth concerns the stupidity of the users, never under estimate it.

    Always check that they have entered the sort of value you require, a number under 256, and deal with anything else.

    It may not matter on this little script, but it is good practise to get into.

    J.C.

      J.C.-

      Thanks for the advice; will apply it.

      Best,

      J.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997265]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-12-20 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls