[opensuse] Quick Perl Question #2 - Why does chomp($float); = "1"?
Listmates, Perl Q # 2 - and counting. Working with perl, I can't figure out why if I read a number (floating point) from STDIN and then chomp($float); it returns 1. Seems crazy. I can remove the new-line by dividing by 1.0, but that just seems like a hack. What say the gurus? Here is the script: #!/usr/bin/perl use warnings; use strict; my $TEMPC; my $TEMPF; print "Please enter a temperature in degrees C: \n"; $TEMPC=<STDIN>; print "\$TEMPC before chomp; $TEMPC\n"; $TEMPC=($TEMPC/1.0); # Works! Removes New Line from Number #$TEMPC=chomp($TEMPC); # Does not work, returns $TEMPC as "1", why? print "\$TEMPC after chomp; $TEMPC\n"; $TEMPF=($TEMPC * 9 / 5 + 32); print qq($TEMPC deg. C = $TEMPF def. F\n); exit 0 -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 03 May 2008 13:13, David C. Rankin wrote:
Listmates,
Perl Q # 2 - and counting. Working with perl, I can't figure out why if I read a number (floating point) from STDIN and then chomp($float); it returns 1. Seems crazy. I can remove the new-line by dividing by 1.0, but that just seems like a hack. What say the gurus? Here is the script:
Chomp is not a function in the sense you're using it. It alters the variable you pass in-place, rather than returning the "comped" value. Change that line to this: chomp($TEMPC); # Works! Removes New Line from Number By the way, there's a typo in the output line.
-- David C. Rankin
Randall "I told you so" Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 03 May 2008 13:22, Randall R Schulz wrote:
...
Chomp is not a function in the sense you're using it. It alters the variable you pass in-place, rather than returning the "comped" value.
Change that line to this:
chomp($TEMPC); # Works! Removes New Line from Number
What "chomp" returns, by the way, is 1 / true to tell you it changed its argument or 0 / false to tell you it did not alter it. Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 03 May 2008 22:25:57 Randall R Schulz wrote:
What "chomp" returns, by the way, is 1 / true to tell you it changed its argument or 0 / false to tell you it did not alter it.
No, it returns the number of characters deleted. Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 03 May 2008 13:42, Anders Johansson wrote:
On Saturday 03 May 2008 22:25:57 Randall R Schulz wrote:
What "chomp" returns, by the way, is 1 / true to tell you it changed its argument or 0 / false to tell you it did not alter it.
No, it returns the number of characters deleted.
And I'm the guy who warns people about generalizing from empirical testing of programming languages and libraries... Well, it certainly applies to me.
Anders
RRS -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
On Saturday 03 May 2008 13:13, David C. Rankin wrote:
Listmates,
Chomp is not a function in the sense you're using it. It alters the variable you pass in-place, rather than returning the "comped" value.
Change that line to this:
chomp($TEMPC); # Works! Removes New Line from Number
By the way, there's a typo in the output line.
-- David C. Rankin
Randall "I told you so" Schulz
[kicking self for a stupid mistake!] Thank you Randall and Anders. I remember reading exactly that -- about 400 pages ago. And I thought I could master this a 2-3 days... Hah! All and all, I really am beginning to enjoy perl. And all of this fun just to avoid having to type gcc between edits.... -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 03 May 2008 09:33:08 pm David C. Rankin wrote:
Thank you Randall and Anders. I remember reading exactly that -- about 400 pages ago. And I thought I could master this a 2-3 days... Hah!
All and all, I really am beginning to enjoy perl. And all of this fun just to avoid having to type gcc between edits....
-- David C. Rankin, J.D., P.E.
Perl is amazing... once you get the hang of it, you can start using the traziilions of packages, routines and functions. It has the biggest collection of modules of any programming language in the world. a must have in any profesionals toolchest... Jerry -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 03 May 2008 22:13:02 David C. Rankin wrote:
$TEMPC=<STDIN>;
print "\$TEMPC before chomp; $TEMPC\n";
$TEMPC=($TEMPC/1.0); # Works! Removes New Line from Number #$TEMPC=chomp($TEMPC); # Does not work, returns $TEMPC as "1", why?
chomp() doesn't return the chomped string, it returns the number of removed characters (in this case probably just '\n') Instead, try just chomp($TEMPC) and don't assign it Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (4)
-
Anders Johansson
-
David C. Rankin
-
Jerome R. Westrick
-
Randall R Schulz