in reply to (s)printf and push
....or do you mean....push( @array, sprintf( "%.5f", "$number\n") );
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
|
|---|