First off ALWAYS "use strict;" it's just a waste not to. At your assignment: "$number = ;" I assume you put in a value here by hand, (hard coded). You should have shown an example of this. If you just type "$number = 01010001" then perl will take it as a hexadecimal number and thus the 16. Basically if you start a number with 0 it is considered to be hex. If however you assign the number like this: $number = '000101010' then it works just fine for me.
-Wayne > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Sent: Sunday, May 01, 2005 9:51 AM > To: [email protected] > Subject: Problem > > 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: > > #!/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 = ; > 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"; > > perl wrong 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 = ; > chomp $number; > > while( ($number / $counter) != 0 ) > { > $number1 = ($number / $counter) % 10; > $total = $total + ($number1 * $counter2); > $counter = $counter * 10; > $counter2 = $counter2 * 16; // changed 2 to 16 > } > > print "The decimal equivalent of ", $number, " is ", $total, "\n"; _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
