Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^3: The error says the value is uninitialized, but it works anyway

by haukex (Chancellor)
on Aug 18, 2019 at 08:22 UTC ( #11104644=note: print w/replies, xml ) Need Help??

in reply to Re^2: The error says the value is uninitialized, but it works anyway
in thread The error says the value is uninitialized, but it works anyway

I really wanted to take a user input and force it to be a floating point number not a string (for math equations), but I could only find a way to take user input and make it an integer int(<STDIN>), but what if they enter a number with a decimal?

You've gotten some good answers already, here is a short one :-) Perl automagically converts between strings and numbers, including decimal numbers. If a variable contains a string that looks like a number, and you use it in an operation that normally involves numbers, Perl will do the conversion for you.

my $input = <STDIN>; chomp($input); # remove newline my $output = $input + 1.23; print "output: $output\n";

Now for example, if you enter "3.45", the output is "output: 4.68". Perl has automatically converted $input from "3.45" to 3.45 for the addition, and converted $output from 4.68 to "4.68" for the "output: $output\n"!

When I look things up, it feels like I'm diving into the deep end and I can't see the shore.

I know the feeling as well - looking things up in the official Perl documentation sometimes has that effect, because you get all the details at once. Fortunately, there are tutorials, for example on this site, as part of the Perl documentation (for example perlintro is a good start), and of course there are books such as Learning Perl that give a slow introduction without overwhelming with all the details.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11104644]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2020-02-26 11:15 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (114 votes). Check out past polls.