Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: question regarding "v" flag of printf

by lightoverhead (Monk)
on May 23, 2013 at 16:22 UTC ( #1034974=note: print w/ replies, xml ) Need Help??


in reply to Re: question regarding "v" flag of printf
in thread question regarding "v" flag of printf

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.


Comment on Re^2: question regarding "v" flag of printf
Download Code
Re^3: question regarding "v" flag of printf
by Anonymous Monk on May 23, 2013 at 16:48 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.

    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.

        Um, whoa :) but it also says This can be useful for displaying ordinal values of characters in arbitrary strings , so to review all it says:
        * interpret the supplied string (argument is string)
        * as a vector of integers (list of integers),
        * one for each character in the string (character by character, integer by integer).
        * ordinal values of characters in arbitrary strings (ordinal means numeric, means ordinal values of chracters )
        *  printf "%vd", "AB\x{100}";           # prints "65.66.256" (A is 65, B is 66, \x{100} is 256 )

        I suppose its better say/link ordinal (ord) values of characters (chr) and give explicit chr/ord example

        Or say ...interpret supplied string as list of integers seperated by a dot. Each character is a positive integer, the ordinal(ord) value of the character (chr).

        $ perl -le " printf qq{v%vd\n}, join q//, map {chr $_} 1000, 44000, 99 +000 , 12 " v1000.44000.99000.12 $ perl -le " printf qq{v%vd\n}, v3.14.16.5 " v3.14.16.5 $ perl -le " printf qq{v%vd\n}, v1.2.3 " v1.2.3 $ perl -le " printf qq{v%vd\n}, chr(1).chr(2).chr(3) " v1.2.3 $ perl -le " print q{v}, join q{.}, ord(chr 1), ord(chr 2), ord(chr 3) + " v1.2.3 $ perl -MData::Dump -e " dd(join q//, chr 1, chr 2, chr 3 ) " "\1\2\3" $ perl -le " printf qq{v%vd\n}, qq{\1\2\3} " v1.2.3 $ perl -le " printf qq{bits are %0*v8b\n}, q{ }, q{123}; " bits are 00110001 00110010 00110011 $ perl -le " printf qq{bits are %0*v8b\n}, q{-}, q{123}; " bits are 00110001-00110010-00110011 $ perl -le " print q{bits are }, join q{-}, unpack q{(B8)*}, q{123} " bits are 00110001-00110010-00110011

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-11-28 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (199 votes), past polls