Home > Archive > PERL Beginners > July 2007 > Precedence?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ryan Dillinger 2007-07-29, 6:59 pm |
| Hello,
I have written this script. I have a good understanding of all but line:
$return = ($end - $start) / $start * 100;
If I start with 1000, and end with say..900 or vise versa. You subtract
first then?
Thank You for your help!
#!/usr/bin/perl
use warnings;
$start = 0;
$end = 0;
$return = 0;
while () {
print 'Enter the starting price ';
chomp($start = <STDIN> );
print 'Enter the current price ';
chomp($end = <STDIN> );
if ($start eq '' or $end eq '') {
print "Either the purchase or the current price is missing\n";
next;
}
if ($start =~ /\D/ or $end =~ /\D/) {
if ($start =~ /\// or $end /\//) {
print "Enter prices as decimal numbers.\n";
next;
}
}
last;
}
$return = ($end - $start) / $start * 100;
if ($start > $end) {
print "Your investment has lost money\n";
}elsif ($start < $end) {
print "Your investment has made money\n";
}else{
print "Your investment lost money\n";
}
print "Your return on your money is: ";
printf("%.1f%%\n", $return);
________________________________________
_________________________
Don't get caught with egg on your face. Play Chicktionary!_
http://club.live.com/chicktionary.a...otmailtextlink2
| |
| Ricky Zhou 2007-07-29, 6:59 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ryan Dillinger wrote:
> Hello,
> I have written this script. I have a good understanding of all but line:
> $return = ($end - $start) / $start * 100;
If you start with 1000 and end with 900, you have:
(900 - 1000) / 1000 * 100 = -100 / 1000 * 100 = -0.1 * 100 = -10
If it were ($end - $start) / ($start * 100), *then* it would be
different (just go by the order of operations, or perldoc perlop).
> #!/usr/bin/perl
> use warnings;
Try using strict as well :)
Hope this helps,
Ricky
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
iD8DBQFGrOezZBKKLMyvSE4RAtSvAJ45NM5C/ojyR4XLwkNFQw6W8QkTjgCgwbew
c8FyKlkVzMAIT5wqUxuaIxQ=
=ctVO
-----END PGP SIGNATURE-----
| |
| yaron@kahanovitch.com 2007-07-29, 6:59 pm |
| See http://www.perl.com/doc/manual/html/pod/perlop.html
Yaron Kahanovitch
----- Original Message -----
From: "Ryan Dillinger" <puppetmyst@hotmail.com>
To: beginners@perl.org
Sent: 21:06:55 (GMT+0200) Africa/Harare =D7=99=D7=95=D7=9D =D7=A8=D7=90=D7=
=A9=D7=95=D7=9F 29 =D7=99=D7=95=D7=9C=D7=99 2007
Subject: Precedence?
Hello,
I have written this script. I have a good understanding of all but line:
$return =3D ($end - $start) / $start * 100;
If I start with 1000, and end with say..900 or vise versa. You subtract=20
first then?
Thank You for your help!
#!/usr/bin/perl
use warnings;
$start =3D 0;
$end =3D 0;
$return =3D 0;
while () {
print 'Enter the starting price ';
chomp($start =3D <STDIN> );
print 'Enter the current price ';
chomp($end =3D <STDIN> );
if ($start eq '' or $end eq '') {
print "Either the purchase or the current price is missing\n";
next;
}
if ($start =3D~ /\D/ or $end =3D~ /\D/) {
if ($start =3D~ /\// or $end /\//) {
print "Enter prices as decimal numbers.\n";
next;
}
}
last;
}
$return =3D ($end - $start) / $start * 100;
if ($start > $end) {
print "Your investment has lost money\n";
}elsif ($start < $end) {
print "Your investment has made money\n";
}else{
print "Your investment lost money\n";
}
print "Your return on your money is: ";
printf("%.1f%%\n", $return);
________________________________________
_________________________
Don't get caught with egg on your face. Play Chicktionary!=EF=BF=BD=20
http://club.live.com/chicktionary.a...otmailtextlink2
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Mr. Shawn H. Corey 2007-07-29, 6:59 pm |
| Ryan Dillinger wrote:
> Hello,
> I have written this script. I have a good understanding of all but line:
> $return = ($end - $start) / $start * 100;
> If I start with 1000, and end with say..900 or vise versa. You subtract
> first then?
What do you expect as an result?
> Thank You for your help!
>
> #!/usr/bin/perl
> use warnings;
>
> $start = 0;
> $end = 0;
> $return = 0;
>
> while () {
> print 'Enter the starting price ';
> chomp($start = <STDIN> );
> print 'Enter the current price ';
> chomp($end = <STDIN> );
> if ($start eq '' or $end eq '') {
> print "Either the purchase or the current price is missing\n";
> next;
> }
>
> if ($start =~ /\D/ or $end =~ /\D/) {
> if ($start =~ /\// or $end /\//) {
if ($start =~ /\// or $end =~ /\//) {
> print "Enter prices as decimal numbers.\n";
> next;
> }
> }
> last;
> }
> $return = ($end - $start) / $start * 100;
> if ($start > $end) {
> print "Your investment has lost money\n";
> }elsif ($start < $end) {
> print "Your investment has made money\n";
> }else{
> print "Your investment lost money\n";
print "Your investment has broken even\n";
> }
> print "Your return on your money is: ";
> printf("%.1f%%\n", $return);
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
|
|
|
|
|