in reply to (s)printf and push

Ummm, of cause it's not going to work, you are passing the wrong argument into sprintf. Try to change to this:
push @array, sprintf "%.5f\n", $number;
The reason why your sprintf didn't work is because sprintf was expecting a floating point number as the second argument (that matches %.5f), while you were passing in a string.

Update: my mistake, as people pointed out, perl is quite happy with the syntax. Only that the string pushed into the array does not contain "\n", which is probably what the monk wanted in the first place.

Replies are listed 'Best First'.
Re: (s)printf and push
by Abigail-II (Bishop) on Sep 30, 2003 at 10:37 UTC
    Trailing non-digits are fine - Perl will ignore them when converting strings to numbers (not just Perl, atoi() and friends allow that too). And if a newline is following the number, Perl won't even emit a warning:
    #!/usr/bin/perl use strict; use warnings; my @array; my $number = "3\n"; push @array, sprintf "%.5f", "$number\n"; print $array [0], "\n"; __END__ 3.00000

    Abigail

Re: Re: (s)printf and push
by bart (Canon) on Sep 30, 2003 at 11:35 UTC
    Oh, it "works" alright, only the formatted string won't contain the newline, which is likely what the OP wanted. Putting the newline where it belongs, in the (s)printf format, like you did, does that trick.