Well "simple terms" were perhaps too simple. This is not true if you have a program statement say $x=32; In the context of the question being asked (inputing a value from the console), this is true and I discussed some of the ramifications of that and gave demo code. In the subroutine that prompts for the float... In version #1, I just strip off the leading and trailing spaces with a regex. The returned value is a string. However, as I point out an alternate way to "strip off the white space" (sort of) is to force Perl to convert the string to a numeric value. You can do this by simply adding 0 to it. $float+=0; When you do this to an int value, it is pretty much like stripping the whitespace. However with a float, there will be usual storage imprecision and possible extension digits after the decimal. 6.3 might be 6.33333333, etc. As I point out, if you had inputted say "+5.0", if you use the "add 0" method, the plus sign will not be there when printed out in the calling routine because Perl will be using the numeric value, not the string value we started with. Of course when inputting a string and doing math upon it, you have to ensure the string is a valid number, else there will be at least some kind of warning message generated!
The important point for the OP which perhaps got lost, is that you don't have to call any special function to cause the conversion to a numeric value - in some languages you do, but not in Perl. (In C you could use a %f format spec for the read and there is no string value, you get a binary number straight away). In Perl there will be a string value and you just do math on that string and Perl will do "the right thing".
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|