formatting question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
sprintf( "%s%$Fmt%s", ("%$Fmt=|", $TestStr, "|"))

This is in Perl for Dummies, 4th ed, p160.

I'm trying to understand this ...

the first part, "%s%$Fmt%s", my understanding is the format part,
which specifies the formats for the second part, thelist part, ("%
$Fmt=|", $TestStr, "|"): %s for "%$Fmt=|", %$Fmt for $TestStr, and %s
for "|", respectively.  Is this correct?

Then what is %$Fmt, it seems a % for format and then a variable $Fmt,
the book did not mention any format string like this ...

Anyone can shed some light?  Thanks!

Re: formatting question

April wrote:
Quoted text here. Click to load it

Double quoted strings are processed and the results of that processing
are then used in the rest of the expression. This applies everywhere in
Perl. Part of that processing is interpolation of variables. This is
part of string handling in general and not specifically part of the job
done by printf() - which is why string-interpolation isn't mentioned in
the documentation for printf.

If $Fmt contains 's' then  "%s%$Fmt%s" is '%s%s%s'

If $TestStr contains 'teststring' the result of the original sprintf()
is then '%s=|teststring|'

Quoted text here. Click to load it

Presumably the author uses this in some sort of loop to illustrate the
effects of various formatting characters.


Re: formatting question

Quoted text here. Click to load it

Thanks RGB, this is very helpful.  Although it is at a higher level to
explain, comparing what Ben described in his post, I think I
understand what you explained.  Thanks you again!

Re: formatting question

Quoted text here. Click to load it

A few more lines of code showing what $Fmt and $TestStr are set to would
be useful... I'm going to guess you were using something like

    my $Fmt     = 'f';
    my $TestStr = '1.004';

in which case you would get the string '%f=|1.004000|' back from
Quoted text here. Click to load it

Hmmm, I would suggest you find a new book. Quite apart from the fact
I've never heard anything good about any of the 'for Dummies' books,
the expression you gave is *not* a clear way to write what they wanted.
'Learning Perl', published by O'Reilly, is the standard recommendation;
for others, see perldoc -q book or .

Quoted text here. Click to load it

Firstly, there is no 'first part' and 'second part', and the inner set
of parens are completely unnecessary. The important point is that
sprintf treats its first argument specially, and uses it to format the

Quoted text here. Click to load it


What you are not understanding is that there are two levels of
intepretation going on here. (This is one of the reasons I would not
write it like that: it's confusing.)

First Perl expands the double-quoted strings, so given the values I was
assuming above sprintf gets passed the list

    '%s%f%s', '%f=|', '1.004', '|'

which I've written with single-quotes to show they aren't going to be
expanded again by Perl. Then sprintf takes the first argument, and
formats the rest in the appropriate places, so

    %s -> %f=|
    %f -> 1.004000
    %s -> |

    %s%f%s -> %f=|1.004000|

I presume the aim here is to let you put in various values for $TestStr,
and various formats, and show you how sprintf would intepret them.

I would have writted it as

    sprintf "%%$Fmt=|%$Fmt|", $TestStr

or perhaps even

    "%$Fmt=|" . sprintf("%$Fmt", $TestStr) . '|'

which, although it still looks a little like line-noise, is probably
easier to understand.


Raise your hand if you're invulnerable.

Re: formatting question

Quoted text here. Click to load it

Thank you very much Ben for spending so much time with details and
examples, you explained really well and I think I do understand this
piece of the language now.  Thanks again Ben!

Site Timeline