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

My output seems to be coming out in reverse order of how it is programmed. Can anyone tell me why? Also, when run it says scalar value @binary1 better written as $binary1, what does that mean? I am running a piece of code which shows output: Program ended successfully In binary, that is 11111111 The code is:
#!C:\Perl\ use warnings; use strict; #binary.plx print"Please enter an integer less than or equal to 256: "; our $decimal = <STDIN>; our @binary = ("0","1","2","3","4","5","6","7","8"); our $x = 0; print"Checkpoint1\n"; if ($decimal == "256") { print "In binary, that is "; while ($x < 8) { @binary[$x]=1; print @binary[$x]; $x++; } die"\n\nProgram ended successfully"; } elsif ($decimal < "256") { @binary[0] = 0; } # Nothing more needs to be done here. elsif ($decimal >"256") { die"\nYou entered an invalid number!\n\n"; } $decimal-=128; print"\ndecimal is ",$decimal; if ($decimal < "128") { @binary[1] = 0; print"\nThe second digit ",@binary[1]; }

Replies are listed 'Best First'.
Re: a newbie's output reversed
by johngg (Canon) on Dec 13, 2011 at 23:40 UTC

    The quick ways to do what I think you are doing are to use either pack and unpack or printf/sprintf. Something more analogous to your method can be done using unshift to build an array of ones and zeros depending on whether the decimal value leaves a remainder when divided by two then shifting it right one bit, doing this eight times then finally joining the array into a string representing the binary value.

    knoppix@Microknoppix:~$ perl -E ' > $dec = 167; > say unpack q{B*}, pack q{c}, $dec; > printf qq{%08b\n}, $dec; > unshift @bits, do { > my $bit = $dec % 2 ? 1 : 0; > $dec >>= 1; > $bit; > } for 0 .. 7; > say join q{}, @bits;' 10100111 10100111 10100111 knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG

Re: a newbie's output reversed
by toolic (Bishop) on Dec 13, 2011 at 22:16 UTC
    My output seems to be coming out in reverse order of how it is programmed.
    I have no idea what you mean by that. Show the output you expect for a given input.
    when run it says scalar value @binary1 better written as $binary1, what does that mean?
    Use perldoc diagnostics to get a more verbose explanation:
    use diagnostics;
    Another tip: Don't use quotes around your numbers when performing numeric comparisons. Change:
    elsif ( $decimal < "256" ) {
    to:
    elsif ( $decimal < 256 ) {
    See also:
    perldoc -q convert
Re: a newbie's output reversed
by NetWallah (Canon) on Dec 14, 2011 at 01:08 UTC
    johngg (++) has given you 3 ways to achieve your results, but these require a little more in-depth perl knowledge.

    Based on your code, this working version should be simpler to understand:

    use warnings; use strict; print"Please enter an integer less than or equal to 256: "; my $decimal = <STDIN>; print "Decimal=$decimal\n"; my @binary = (0) x 8; # Initialize to 8 zeros if ($decimal >"256") { die"\nYou entered an invalid number!\n\n"; } for (0..7){ $binary[7 -$_] = 1 if $decimal & 2**$_; # Mask out the bit, and set + $binary[n] } print"@binary\n";

                "XML is like violence: if it doesn't solve your problem, use more."

Re: a newbie's output reversed
by Anonymous Monk on Dec 14, 2011 at 08:50 UTC
    print "In binary, that is "; while ($x < 8) { @binary[$x]=1; print @binary[$x]; $x++; } die"\n\nProgram ended successfully";

    The warning means that @binary[$x] = 1; should be written as $binary[$x] = 1;

    The reverse order comes from buffering. die writes to a different buffer (STDERR) than regular prints (which write to STDOUT). In fact, STDERR is unbuffered by default, and STDOUT is line-buffered by default. That is, everything you print to STDERR is immediately shown on screen, while everything to STDOUT will not show up on the screen until a line break is printed.

    To end a program "successfully," you should not use die. Instead, you should print whatever you wanted to with print and then exit(0);

      And since you are doing a programming assignment (the other posters did not seem to realise that), the path you are heading for asks for copy-pasting and thus is inefficient. What you instead should do is compare the value in a loop and divide by two (rounding down) after each loop iteration. This shall give you the binary form, and you don't have to code 256, 128, 64 etc in your code.

      Of course, that may not be the best approach. I think the book asked for how to use the & (binary AND) operator; let's see how it behaves:

      $input = 5; $input & 4 == 1; $input & 2 == 0; $input & 1 == 1;

      That looks like the binary form of "5" to me.