As others have pointed out, the "\n" contained in the string, "$number\n" basically just falls into the bitbucket since it has nowhere better to go in the string to number conversion that's taking place. Perl would happily do this for you without complaining because you aren't placing "use warnings;" at the top of your program (be sure that "use strict;" is there too!). You also have the potential for order of operations problems, at least from a readability standpoint. For example, do you mean:
push( @array, sprintf( "%.5f", "$number\n") );
....or do you mean....
push( @array, (sprintf "%.5f"), "$number\n");
Of course the latter makes no sense at all, and Perl gets it right too. But in more complex constructs a bunch of parameter lists flattened out into one long list separated by commas, with no parenthesis, can get a little confusing.
As far as how to solve your immediate problem, if what you really want is to push a list of numbers, formatted, with a newline at the end of each number, what you really meant to say was:
push @array, sprintf( "%.5f", $number) . "\n";
Note the use of the concatenation operator, ".", which essentially appends "\n" to the string created by your call to sprintf. Then the entire string (the results of sprintf, concatenated with "\n" at the end) gets pushed onto @array by push.
You could also have done it like this:
push @array, sprintf( "%.5f%s", $number, "\n");
In which case you're providing multiple fields to be pasted into the format string in sprintf.
On a non-technical note, you also really meant to say (instead of "doesn't work") that "It doesn't do what I expected.". You would then go on to explain what you expected and what you got instead. "doesn't work" usually fails to accurately describe the problem, and ambiguity in describing problems is almost as difficult to decipher as the ambiguity that often created the problem in the first place.
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
|