Re: sprintf and printf in Perl



Hello,

The simple difference is,'sprintf' format the strings and pass the result to a variable.

$ perl -e 'my $var=sprintf "%30s\n","hello,world";print $var'
hello,world

While 'printf' print the result directly to screen.

$ perl -e 'printf "%30s\n","hello,world"'
hello,world

Hope it helps.

-jeff

-----Original Message-----
From: chen_li3@xxxxxxxxx
To: beginners@xxxxxxxx
Sent: Thu, 14 Sep 2006 9:44 PM
Subject: sprintf and printf in Perl


Hi all,

First of all I want to thank you all for reading and
replying my previous posts.

Now I come across to another problem:

I want to print out a AoA. If I use printf I get what
I expect. If I use sprintf for string I get error
messages but the remaining still look OK. But If use
sprintf also for numbers I get nothing only error
messages. I look at the POD. It looks like sprintf and
printf are changeable to me. Any comments?

Thanks,

Li

#!c:/Perl/bin/perl.exe

use warnings;
use strict;
use Data::Dumper;

my @array=(
['A',1,2,3,4,5,6,7],
['B',1,2,3,4,5,6,7],
['C',1,2,3,4,5,6,7],

);

foreach my $r(@array){
foreach (@$r){
if (/\d+/){printf "%10.2f",$_ ;
}else{printf "%10s",$_;}

}
print "
";
}

#############################################
Output1 from screen (use printf only and expected)

A 1.00 2.00
B 1.00 2.00
C 1.00 2.00

Output2 from screen (use sprintf for string)

Useless use of sprintf in void context at math9.pl
line 18
1.00 2.00
1.00 2.00
1.00 2.00

Output3 from screen (use sprintf for both)
Useless use of sprintf in void context at math9.pl
line 18.
Useless use of sprintf in void context at math9.pl
line 18.




__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
<http://learn.perl.org/> <http://learn.perl.org/first-response>
________________________________________________________________________
Check out the new AOL. Most comprehensive set of free safety and security tools, free access to millions of high-quality videos from across the web, free AOL Mail and more.


Relevant Pages

  • Re: probem with dl_runtime.c
    ... >> Well, it would involve you declaring a float followed by a string, then ... > That will cause printf() to crash, and there is no argument it can. ... > We aren't talking about sprintf(). ... in itself can corrupt malloc, ...
    (comp.os.linux.development.system)
  • setting MAX characters emitted in printf() functions?
    ... Where printf() here is generic and refers to sprintf, fprintf, etc. ... 256 characters on some platforms a double is even larger than that, ... string containing the formatted variable if the desired field ...
    (comp.lang.c)
  • Re: setting MAX characters emitted in printf() functions?
    ... >Where printf() here is generic and refers to sprintf, fprintf, etc. ... C99 snprintf returns the number of characters that would have been ...
    (comp.lang.c)
  • Re: function within qq{}
    ... >>> before the string is built? ... WB> Use printf. ... whereas i use sprintf when i need too. ...
    (comp.lang.perl.misc)
  • Re: setting MAX characters emitted in printf() functions?
    ... > Where printf() here is generic and refers to sprintf, fprintf, etc. ... > 256 characters on some platforms a double is even larger than that, ... > string containing the formatted variable if the desired field ...
    (comp.lang.c)