Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: question regarding "v" flag of printf

by rjt (Deacon)
on May 23, 2013 at 05:15 UTC ( #1034866=note: print w/ replies, xml ) Need Help??


in reply to question regarding "v" flag of printf

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.


Comment on Re: question regarding "v" flag of printf
Select or Download Code
Re^2: question regarding "v" flag of printf
by lightoverhead (Monk) on May 23, 2013 at 16:22 UTC

    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?

        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)"

        and it's actually as rjt said,"Perl interprets each character in the string as its index in your character set."

        it's "index",not the number in the string.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1034866]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-11-23 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls