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


In reply to Re: (s)printf and push by davido
in thread (s)printf and push by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.