slicks1 slicks1 - 1 year ago 67
Perl Question

Cannot get correct return value in sprintf function call

My problem is that the return value im printing is not being translated into a value.

I have the following code in a test file.

#!/usr/bin/perl -I/srv/www/jonathan/m/www
my $var = sprintf("$%.1f lbs",(77*2.20462));
print $var;

Its returning:
instead of the value i need to see.

What am i doing incorrectly here? I'm a perl newbie.

Answer Source

Perl uses dolar($) symbol to declare a scalar variable, you need to remove the $ symbol from the sprintf.

corrected code

my $var = sprintf("%.1f lbs",(77*2.20462));
print $var;


169.8 lbs
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download