Re: sprintf and printf in Perl
- From: jeffhua@xxxxxxx
- Date: Thu, 14 Sep 2006 09:52:24 -0400
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.
- References:
- sprintf and printf in Perl
- From: Chen Li
- sprintf and printf in Perl
- Prev by Date: Re: hash access
- Next by Date: Archive::Zip and hash issues
- Previous by thread: sprintf and printf in Perl
- Next by thread: Re: sprintf and printf in Perl
- Index(es):
Relevant Pages
|