Re: sprintf format question

[email protected] (Rob Dixon)
Newsgroups perl.beginners
Message-ID <[email protected]>
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 ...
> 
> Anyone can shed some light?  Thanks!

Hi April

The value of $Fmt will be interpolated into the double-quoted sting before it is
used as a format. It needs to be one of the single-character format conversion.
If you're not clear what I mean then try running this piece of code.

for my $Fmt ('s', 'd') {
  print "%s%$Fmt%s", "\n";
}

HTH,

Rob
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.