Click here to get back home

formatting question

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
formatting question April 06-03-2008
Posted by April on June 3, 2008, 1:58 pm
Please log in for more thread options
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!

Posted by RedGrittyBrick on June 3, 2008, 3:48 pm
Please log in for more thread options
April wrote:
> 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 ...

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|'

>
> Anyone can shed some light? Thanks!

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


--
RGB

Posted by April on June 3, 2008, 8:44 pm
Please log in for more thread options
wrote:
> April wrote:
> > sprintf( "%s%$Fmt%s", ("%$Fmt=3D|", $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=3D|", $TestStr, "|"): %s for "%$Fmt=3D|", %$Fmt for $TestStr, and %=
s
> > for "|", respectively. =A0Is 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 ...
>
> 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 =A0"%s%$Fmt%s" is '%s%s%s'
>
> If $TestStr contains 'teststring' the result of the original sprintf()
> is then '%s=3D|teststring|'
>
>
>
> > Anyone can shed some light? =A0Thanks!
>
> Presumably the author uses this in some sort of loop to illustrate the
> effects of various formatting characters.
>
> --
> RGB

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!

Posted by Ben Morrow on June 3, 2008, 4:50 pm
Please log in for more thread options

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

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
sprintf.

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

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 http://books.perl.org .

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

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
rest.

> %s for "%$Fmt=|", %$Fmt for $TestStr, and %s for "|", respectively. Is
> this correct?

Yes.

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

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.

Ben

--
Raise your hand if you're invulnerable.
[ben@morrow.me.uk]

Posted by April on June 3, 2008, 8:48 pm
Please log in for more thread options
>
> > sprintf( "%s%$Fmt%s", ("%$Fmt=3D|", $TestStr, "|"))
>
> 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
>
> =A0 =A0 my $Fmt =A0 =A0 =3D 'f';
> =A0 =A0 my $TestStr =3D '1.004';
>
> in which case you would get the string '%f=3D|1.004000|' back from
> sprintf.
>
> > This is in Perl for Dummies, 4th ed, p160.
>
> 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 orhttp://books.perl.org.
>
> > 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=3D|", $TestStr, "|"):
>
> 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
> rest.
>
> > %s for "%$Fmt=3D|", %$Fmt for $TestStr, and %s for "|", respectively. Is=

> > this correct?
>
> Yes.
>
> > Then what is %$Fmt, it seems a % for format and then a variable $Fmt,
> > the book did not mention any format string like this ...
>
> 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
>
> =A0 =A0 '%s%f%s', '%f=3D|', '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
>
> =A0 =A0 %s -> %f=3D|
> =A0 =A0 %f -> 1.004000
> =A0 =A0 %s -> |
>
> =A0 =A0 %s%f%s -> %f=3D|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
>
> =A0 =A0 sprintf "%%$Fmt=3D|%$Fmt|", $TestStr
>
> or perhaps even
>
> =A0 =A0 "%$Fmt=3D|" . sprintf("%$Fmt", $TestStr) . '|'
>
> which, although it still looks a little like line-noise, is probably
> easier to understand.
>
> Ben
>
> --
> Raise your hand if you're invulnerable.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0[b...@morrow.me.uk]

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!


Similar ThreadsPosted
Perl-ish mail formatting question regarding "\t" October 7, 2007, 11:18 am
newbie question on output formatting/"pretty" print April 16, 2006, 10:59 pm
pod and formatting? January 18, 2005, 8:49 pm
About formatting April 30, 2005, 7:20 pm
GD Graph Pie Formatting November 30, 2004, 11:56 pm
Date formatting June 28, 2006, 12:26 pm
CGI & Formatting or a Perl Problem?! May 2, 2005, 1:10 pm
Formatting XML output from a hashref August 23, 2005, 2:43 pm
Source code formatting January 28, 2006, 8:13 pm
change mac address formatting in a regex December 19, 2004, 7:33 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap