in reply to a newbie's output reversed

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);

Replies are listed 'Best First'.
Re^2: a newbie's output reversed
by Anonymous Monk on Dec 14, 2011 at 08:59 UTC

    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.