in reply to Need a little coding help
The our is lexically scoped. Move it out of the if.
I thought this meant that the variable $bin1 I declared was not global
Quite the opposite, our means you want to use the global variable. There's no reason to use our here. There's almost never a reason to use it. You want to create a variable, so you should be using my.
my $bin1; if ($binc1 != 0) { $bin1 = 1; } else { $bin1 = 0; }
The else clause is executed when $binc1 isn't not equal to zero. Avoiding the double negative:
my $bin1; if ($binc1 == 0) { $bin1 = 0; } else { $bin1 = 1; }
But you could just simplify instead:
my $bin1; if ($binc1) { $bin1 = 1; } else { $bin1 = 0; }
Simplified further:
my $bin1 = $binc1 ? 1 : 0;
Since the purpose is to format the bit for output, we actually want a string:
ormy $bin1 = $binc1 ? '1' : '0';
print "The number in binary is: ", ($binc1 ? '1' : '0'), "\n";
Update: Fixed problem identified in reply.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need a little coding help
by samaniac (Initiate) on Nov 25, 2010 at 20:19 UTC | |
by ikegami (Patriarch) on Nov 25, 2010 at 21:12 UTC |