sprintf '%.5s', 123456789

Discussion in 'Perl Misc' started by Ilya Zakharevich, Sep 10, 2009.

  1. I added this functionality to (an unreleased version) of MP3::Tag, but
    should not it be made part of main Perl? Like in

    perl -wle "printf '%.5s', 123456789"
    1.2e9

    ( Let me remind that [rarely used] constraint `.5' means maxwidth=5. )

    Just thinking aloud (and not in compatibility-police mode),
    Ilya
    Ilya Zakharevich, Sep 10, 2009
    #1
    1. Advertising

  2. Ilya Zakharevich

    Guest

    On Thu, 10 Sep 2009 21:40:35 +0000 (UTC), Ilya Zakharevich <> wrote:

    >I added this functionality to (an unreleased version) of MP3::Tag, but
    >should not it be made part of main Perl? Like in
    >
    > perl -wle "printf '%.5s', 123456789"
    > 1.2e9
    >
    >( Let me remind that [rarely used] constraint `.5' means maxwidth=5. )
    >
    >Just thinking aloud (and not in compatibility-police mode),
    >Ilya


    Thats slick.
    Btw:
    printf "%.1e\n", 123456789;
    1.2e+008

    -sln
    , Sep 11, 2009
    #2
    1. Advertising

  3. Ilya Zakharevich wrote:
    > I added this functionality to (an unreleased version) of MP3::Tag, but
    > should not it be made part of main Perl? Like in
    >
    > perl -wle "printf '%.5s', 123456789"
    > 1.2e9


    $ perl -wle "printf '%.5s', 123456789; print ''"
    12345

    Maybe your shell is interpreting something? Try using single quotes
    around the -e expression.


    John
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
    John W. Krahn, Sep 11, 2009
    #3
  4. On 2009-09-10, Ben Morrow <> wrote:
    >> perl -wle "printf '%.5s', 123456789"
    >> 1.2e9
    >>
    >> ( Let me remind that [rarely used] constraint `.5' means maxwidth=5. )


    > Dispatching based on PV vs NV is, as you know well, a very dodgy
    > proposition in Perl 5.


    In MP3::Tag, same happens for

    perl -wle "printf '%.5s', '123456789'"

    Yours,
    Ilya
    Ilya Zakharevich, Sep 11, 2009
    #4
  5. On 2009-09-10, David Harmon <> wrote:
    > On Thu, 10 Sep 2009 21:40:35 +0000 (UTC) in comp.lang.perl.misc, Ilya
    > Zakharevich <> wrote,
    >>I added this functionality to (an unreleased version) of MP3::Tag, but
    >>should not it be made part of main Perl? Like in
    >>
    >> perl -wle "printf '%.5s', 123456789"
    >> 1.2e9


    > Since %s says to treat it like a string, how can it legitimately insert
    > irrelevant period and 'e' characters?


    "1.2e9" is "a string". So is "123456789". As far as "treating as a
    string" goes, what do you think is the EXACT MESSAGE of this phrase?

    [I trust that some C/POSIX standard would explicitly mention that
    %.[WIDTH]s shortens by "truncation on the right". I would be very
    much surprised if Perl docs mention the exact semantic of shortening,
    and/or relation to C or POSIX...]

    Yours,
    Ilya
    Ilya Zakharevich, Sep 11, 2009
    #5
  6. On 2009-09-10 23:43, <> wrote:
    > On Thu, 10 Sep 2009 21:40:35 +0000 (UTC), Ilya Zakharevich <> wrote:
    >>I added this functionality to (an unreleased version) of MP3::Tag, but
    >>should not it be made part of main Perl? Like in
    >>
    >> perl -wle "printf '%.5s', 123456789"
    >> 1.2e9
    >>
    >>( Let me remind that [rarely used] constraint `.5' means maxwidth=5. )
    >>
    >>Just thinking aloud (and not in compatibility-police mode),

    >
    > Thats slick.


    s/l//;

    hp
    Peter J. Holzer, Sep 11, 2009
    #6
  7. On 2009-09-11 08:04, Ilya Zakharevich <> wrote:
    > On 2009-09-10, David Harmon <> wrote:
    >> On Thu, 10 Sep 2009 21:40:35 +0000 (UTC) in comp.lang.perl.misc, Ilya
    >> Zakharevich <> wrote,
    >>>I added this functionality to (an unreleased version) of MP3::Tag, but
    >>>should not it be made part of main Perl? Like in
    >>>
    >>> perl -wle "printf '%.5s', 123456789"
    >>> 1.2e9


    Definitely not.


    >> Since %s says to treat it like a string, how can it legitimately insert
    >> irrelevant period and 'e' characters?

    >
    > "1.2e9" is "a string". So is "123456789". As far as "treating as a
    > string" goes, what do you think is the EXACT MESSAGE of this phrase?
    >
    > [I trust that some C/POSIX standard would explicitly mention that
    > %.[WIDTH]s shortens by "truncation on the right".


    Yes.

    > I would be very much surprised if Perl docs mention the exact
    > semantic of shortening, and/or relation to C or POSIX...]


    perldoc -f sprintf:

    | For string conversions, specifying a precision truncates
    | the string to fit in the specified width:
    |
    | printf '<%.5s>', "truncated"; # prints "<trunc>"
    | printf '<%10.5s>', "truncated"; # prints "< trunc>"

    It doesn't say "truncation on the right", but it says "truncate", and
    the examples are specific enough. And in any case, turning "123456789"
    into "1.2e9" doesn't count as "truncate" by any definition.

    hp
    Peter J. Holzer, Sep 11, 2009
    #7
  8. >>>>> "Ben" == Ben Morrow <> writes:

    Ben> I believe Ilya was suggesting it as a potential feature, rather than
    Ben> describing current behaviour. I presume the intention was that %s should
    Ben> choose string/numeric truncation based on whether the value was string
    Ben> or numeric? For reasons we've gone over recently, this is a bad idea.

    Agreed. If I ever get different results from:

    printf "%.5s", $x;

    and

    printf "%.5s", "$x";

    I'll know that Perl has jumped the shark.

    print "Just another Perl hacker,"; # the original

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
    Randal L. Schwartz, Sep 11, 2009
    #8
  9. On 2009-09-11, Ben Morrow <> wrote:
    >> > Dispatching based on PV vs NV is, as you know well, a very dodgy
    >> > proposition in Perl 5.

    >>
    >> In MP3::Tag, same happens for
    >>
    >> perl -wle "printf '%.5s', '123456789'"

    >
    > ...so, this is just a variation on %g?


    I would prefer to avoid vague-y statements here. The question may be
    too controvercial even without them. ;-)

    - At least it is currently implemented via trying %g with different
    precisions, and postprocessing the results.

    (Of course, this is not the first time I needed a functionality. As
    Larry said it, I needed it more than 3 times in different contexts -
    before I brought it here.)

    Yours,
    Ilya
    Ilya Zakharevich, Sep 11, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    ATDT 123456789 How??

    Guest, Aug 30, 2004, in forum: Java
    Replies:
    2
    Views:
    600
    Tor Iver Wilhelmsen
    Aug 31, 2004
  2. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,584
    shea martin
    Sep 3, 2004
  3. Pep
    Replies:
    5
    Views:
    4,049
  4. A.M
    Replies:
    9
    Views:
    629
    Warren Block
    Jun 3, 2006
  5. 123456789

    , Apr 15, 2009, in forum: C++
    Replies:
    0
    Views:
    352
Loading...

Share This Page