need some help with writing a basic program
need some help with writing a basic program
am 12.09.2011 20:55:26 von Ryan Munson
--B_3398684191_28249534
Content-type: text/plain;
charset="US-ASCII"
Content-transfer-encoding: 7bit
Hi all,
I am running through a good book on Perl and am having some issues with a
script I am trying to create for one of the practice exercises (I am
teaching myself Perl for work). Just to let you know the solution does not
involve using an if statement because this is in the next chapter.
The objective is to "Write a program that asks for a decimal less than 256
and converts it to binary."
Any help would be greatly appreciated!
#!/usr/bin/perl
# dec-less-than-exercise.pl
use strict;
use warnings;
print "Decimal Less Than 1.0\n\nPlease enter a decimal number less than 256:
";
chomp(my $dec = );
$dec = $dec < 256;
print $dec, "is less than 256! ", "\n";
$dec = oct($dec);
print $dec, "is your binary value. ", "\n";
Ryan Munson
--B_3398684191_28249534--
Re: need some help with writing a basic program
am 12.09.2011 21:15:25 von Ryan Munson
--B_3398685329_28319846
Content-type: text/plain;
charset="US-ASCII"
Content-transfer-encoding: 7bit
I could, but the book also mentions a hint to using the bitwise operator.
Loops are still to come. =)
Ryan
From: Hal Wigoda
Date: Mon, 12 Sep 2011 14:07:18 -0500
To: Ryan Munson
Subject: Re: need some help with writing a basic program
Use a while or for loop
On Sep 12, 2011 1:58 PM, "Ryan Munson" wrote:
> Hi all,
>
> I am running through a good book on Perl and am having some issues with a
> script I am trying to create for one of the practice exercises (I am
> teaching myself Perl for work). Just to let you know the solution does not
> involve using an if statement because this is in the next chapter.
>
> The objective is to "Write a program that asks for a decimal less than 256
> and converts it to binary."
>
> Any help would be greatly appreciated!
>
> #!/usr/bin/perl
> # dec-less-than-exercise.pl
>
> use strict;
> use warnings;
>
> print "Decimal Less Than 1.0\n\nPlease enter a decimal number less than 256:
> ";
>
> chomp(my $dec = );
> $dec = $dec < 256;
> print $dec, "is less than 256! ", "\n";
> $dec = oct($dec);
> print $dec, "is your binary value. ", "\n";
>
> Ryan Munson
>
>
>
--B_3398685329_28319846--
Re: need some help with writing a basic program
am 12.09.2011 21:29:03 von Brandon McCaig
On Mon, Sep 12, 2011 at 2:55 PM, Ryan Munson
wrote:
> I am running through a good book on Perl and am having some issues with a
> script I am trying to create for one of the practice exercises (I am
> teaching myself Perl for work). Just to let you know the solution does not
> involve using an if statement because this is in the next chapter.
What have you learned so far? This seems a little advanced to do
before you even know how to use an if statement, unless the book has
already explained the solution and just wants for you to type it out.
> The objective is to "Write a program that asks for a decimal less than 256
> and converts it to binary."
*snip*
> chomp(my $dec = );
> $dec = $dec < 256;
I doubt that this is doing what you intended it to do. The < operator
is a comparison operator (numeric less-than operator) and will return
a boolean result indicating whether or not the left-hand side is
less-than the right-hand side in a numeric context. You're overwriting
your $dec variable with this boolean result, meaning it will either
contain '' (equivalent to zero) or 1 after this statement. You can
confirm this with a simple print statement:
print STDERR "After the less-than operation \$dec = $dec ... :(\n";
> print $dec, "is less than 256! ", "\n";
This looks like the kind of print statement that you would have
/inside/ of an if statement i.e., only if some condition is true, such
as the less-than operation seen erroneously on the previous line.
Perhaps the previous line was supposed to be an if statement? That
would seem to contradict you not being taught about if statements yet,
however.
> $dec = oct($dec);
There are two problems with this line. Firstly, oct refers to the
octal numbering system, which is not the same as the binary numbering
system. (Reading the perldoc, however, I'm surprised to learn that it
can also handle hexadecimal and binary strings, so perhaps this
inconsistency isn't really a problem at all). Secondly, oct doesn't
convert a number to an octal string; rather it converts an octal
string to a number. :) You can see for yourself with the following
command: perldoc -f oct.
You can also see with this simple program:
#!usr/bin/perl
use strict;
use warnings;
# Outputs 8 instead of 12 (the octal number for decimal 10)
# or 10 or whatever other number you might otherwise expect.
print oct(10), "\n";
__END__
Rethink the exercise and what you've learned and let us know if you
still need help. :)
--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: need some help with writing a basic program
am 12.09.2011 21:47:46 von Ryan Munson
The full exercise states:
Write a program that asks for a decimal number less than 256 and converts
it to binary. (Hint: You may want to use the bitwise and operator 8 times.)
Since 256 is 2^8, I am assuming using a computation of 2**8 is involved.
Ryan
On 9/12/11 3:29 PM, "Brandon McCaig" wrote:
>On Mon, Sep 12, 2011 at 2:55 PM, Ryan Munson
> wrote:
>> I am running through a good book on Perl and am having some issues with
>>a
>> script I am trying to create for one of the practice exercises (I am
>> teaching myself Perl for work). Just to let you know the solution does
>>not
>> involve using an if statement because this is in the next chapter.
>
>What have you learned so far? This seems a little advanced to do
>before you even know how to use an if statement, unless the book has
>already explained the solution and just wants for you to type it out.
>
>> The objective is to "Write a program that asks for a decimal less than
>>256
>> and converts it to binary."
>*snip*
>> chomp(my $dec = );
>> $dec = $dec < 256;
>
>I doubt that this is doing what you intended it to do. The < operator
>is a comparison operator (numeric less-than operator) and will return
>a boolean result indicating whether or not the left-hand side is
>less-than the right-hand side in a numeric context. You're overwriting
>your $dec variable with this boolean result, meaning it will either
>contain '' (equivalent to zero) or 1 after this statement. You can
>confirm this with a simple print statement:
>
>print STDERR "After the less-than operation \$dec = $dec ... :(\n";
>
>> print $dec, "is less than 256! ", "\n";
>
>This looks like the kind of print statement that you would have
>/inside/ of an if statement i.e., only if some condition is true, such
>as the less-than operation seen erroneously on the previous line.
>Perhaps the previous line was supposed to be an if statement? That
>would seem to contradict you not being taught about if statements yet,
>however.
>
>> $dec = oct($dec);
>
>There are two problems with this line. Firstly, oct refers to the
>octal numbering system, which is not the same as the binary numbering
>system. (Reading the perldoc, however, I'm surprised to learn that it
>can also handle hexadecimal and binary strings, so perhaps this
>inconsistency isn't really a problem at all). Secondly, oct doesn't
>convert a number to an octal string; rather it converts an octal
>string to a number. :) You can see for yourself with the following
>command: perldoc -f oct.
>
>You can also see with this simple program:
>
>#!usr/bin/perl
>
>use strict;
>use warnings;
>
># Outputs 8 instead of 12 (the octal number for decimal 10)
># or 10 or whatever other number you might otherwise expect.
>print oct(10), "\n";
>
>__END__
>
>Rethink the exercise and what you've learned and let us know if you
>still need help. :)
>
>
>--
>Brandon McCaig
>V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
>Castopulence Software
>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: need some help with writing a basic program
am 12.09.2011 22:02:10 von Brandon McCaig
On Mon, Sep 12, 2011 at 3:47 PM, Ryan Munson
wrote:
> it to binary. (Hint: You may want to use the bitwise and operator 8 times.)
>
> Since 256 is 2^8, I am assuming using a computation of 2**8 is involved.
The answer is in the hint. What does the bitwise-and operator give
you? See perldoc perlop and/or Google for it. It does seem a bit
strange to have you doing bitwise operations before you learn of the
if statement, however ...
--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: need some help with writing a basic program
am 12.09.2011 22:13:41 von Jim Gibson
On 9/12/11 Mon Sep 12, 2011 12:47 PM, "Ryan Munson"
scribbled:
> The full exercise states:
>
> Write a program that asks for a decimal number less than 256 and converts
> it to binary. (Hint: You may want to use the bitwise and operator 8 times.)
>
> Since 256 is 2^8, I am assuming using a computation of 2**8 is involved.
I don't see where 2**8 is involved anywhere.
Decimal and binary are two different ways of representing integer numbers.
The number represented by decimal '17' is also represented by binary
'010001' ( and octal '21').
The 'bitwise-and' operator allows you to query the status of individual bits
in the binary representation of a number. If your decimal number has been
assigned to the Perl variable $dec, then the expression ($dec & 1) will be
true if the lowest bit of the binary form of $dec is 1. Likewise, ($dec & 2)
will be true if the second-lowest bit of $dec is 1, and so on up to ($dec &
128). Since your number is supposed to be "less than 256", i.e. 255 or less,
you will only need 8 bits to represent all possible binary numbers (from 0
to 255).
Normally, rather than doing 8 separate similar things, a programmer will
write a loop that is iterated 8 times. However, if your book says or implies
that you do this exercise without if statements or loops, you may have to
have 8 different explicit expressions using the bitwise-and, each statement
testing a different bit of $dec.
With this information and what Brandon has given you, can you attempt to
rewrite your program?
Cheat1: Perl printf has the %b descriptor that will print any integer in
binary form.
Cheat2: You are not the first person to have trouble with this exercise:
Good luck!
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: need some help with writing a basic program
am 12.09.2011 23:43:08 von Rob Dixon
On 12/09/2011 20:47, Ryan Munson wrote:
>
> The full exercise states:
>
> Write a program that asks for a decimal number less than 256 and converts
> it to binary. (Hint: You may want to use the bitwise and operator 8 times.)
>
> Since 256 is 2^8, I am assuming using a computation of 2**8 is involved.
You must understand the way Perl operators function, and it seems that
you have been shown how to use bitwise 'and' operator '&'.
You have a good idea with the value of 256, but it looks like you have
not understood the binary system properly. The concept of 'masking' bits
by 'anding' them is central to Computer Science.
Good luck
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/