[EMAIL PROTECTED] wrote:
> May anyone tell me if this program is wrong or if there is a
> implementation problem in perl 5.8.6, 5.8.5, 5.8.2
> I will show the perl version and a c++ version. The c++ version
> worked. I will also show the version of the perl version that
> worked with a number changed.
>
>
> The correct version of perl:
You mean working version.
> #!/usr/bin/perl
> # Input an integer containing only 0s and 1s and print its decimal
> equivalent
>
> $counter = 1;
> $counter2 = 1;
> $total = 0;
>
> print "Enter a binary number\n";
> $number = <STDIN>;
> chomp $number;
>
> while( ($number / $counter) != 0 )
> {
> $number1 = ($number / $counter) % 10;
> $total = $total + ($number1 * $counter2);
> $counter = $counter * 10;
> $counter2 = $counter2 * 2;
> }
>
> print "The decimal equivalent of ", $number, " is ", $total, "\n";
This seems simpler:
use strict;
foreach (@ARGV) {
warn "Illegal binary number: $_\n" and next if not /^[01]+$/;
my $total = 0;
$total = $total * 2 + $_ foreach split '', $_;
print "The decimal equivalent of ", $_, " is ", $total, "\n";
}
__END__
> perl wrong version:
Don't use it.
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Unix-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs