Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

question regarding "v" flag of printf

by lightoverhead (Pilgrim)
on May 23, 2013 at 05:08 UTC ( [id://1034863]=perlquestion: print w/replies, xml ) Need Help??

lightoverhead has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a question regarding the "v" flag for "printf".

printf "<%vd>","123";

I thought the result should be "<1.2.3>", but instead it was "<49.50.51>".

As it was described, "This flag tells Perl to interpret the supplied string as a vector of integers, one for each character in the string. Perl applies the format to each integer in turn, then joins the resulting strings with a separator (a dot . by default)"

I assume "123" will be divided as vector of ("1","2","3"), but it became ("49","50","51").

I am confused!

in addition if I want printf to output result of "100 200 300" of string "100200300" what should I do?

Thanks you!

Replies are listed 'Best First'.
Re: question regarding "v" flag of printf
by rjt (Curate) on May 23, 2013 at 05:15 UTC

    Perl interprets each character in the string as its index in your character set. (In ASCII, character 49 is "1").

    To split a string of digits, use:

    my @digits = split //, '123';

    For your second case, there are a few ways to do it, but unpack is a popular method:

    my @nums = unpack '(A3)*', '100200300';

    Note that both of these solutions don't actually care whether the strings contain digits or other characters like letters; if that matters to you, be sure to validate your input.

      Thank you rjt. Now it's clear to me how "v" flag works. The documentation for this flag is so obscure and it needs to be clarified.

      I have seen some code as:

      printf "%vd","\x5\xE\x2"; # print 5.14.2

      how does perl interpret "\" here? It seems it considers it as escape for "x" instead of a single character.

      but why put "\x" before each of these characters ("5", "E", "2")?

      Thank you very much.

        Thank you rjt. Now it's clear to me how "v" flag works. The documentation for this flag is so obscure and it needs to be clarified.

        Um, can you quote which lines are obscure and which need clarification?

Re: question regarding "v" flag of printf
by choroba (Cardinal) on May 23, 2013 at 05:56 UTC
    This works:
    printf '<%vd>', pack 'c*', unpack '(A1)*', '123';
    First, it splits the string into one-character strings (A1), it then replaces each with the number the character represents (c*), which makes it ready for your printf template.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1034863]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-20 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found