sprintf and printf are commands really useful IMO.
http://www.perldoc.com/perl5.6.1/pod/func/sprintf.html
it formats the output to the type you write beside the % sign.
in ANSI C you can only use those (printf and scanf) to get input and
output.
lets say you want to put your times with 04 instead of 4am.. $time =
sprintf("%02d",$time);
often also it's useful to take shortcuts, you don't have to declare
variables..
let's say you have a total number of results for an exam.. and the number
of students that failed.. (%% emans you want to print a litteral %)
my $total = 79;
my $failed = 36;
printf(%d passed this exam (%.2f%%), %d failed (%.2f%%) on a total of %d
students", $total-$failed, (($total-$failed)/$total)*100, $failed,
($failed/$total)*100, $total);
this will return:
43 passed this exam (54.43%), 36 failed (45.57%) on a total of 79
students
you send to printf or sprintf the strinf with the appropirate % types..
the the list of the values, separated by commas...
in this examble I did not have to set a value for both percentages and
for the number for passed exams.
try it it's pretty great :-)
Etienne
Pete Emerson wrote:
> I wonder why the use of sprintf? I would have done it this way:
> foreach (@list) { $_=":$_"; }
>
> Is there something that the sprintf does that I'm missing, or is this
> just another way to skin the same cat? Perhaps in other circumstances
> sprintf is superior?
>
> Just curious,
> Pete
>
> Etienne Marcotte wrote:
> for (1..@list) { @list[$_] = sprintf(":%d",@list[$_]; }
>
> David Wagner wrote: (I edited to compact it)
> foreach( @aaa ) { my $MyId = sprintf "\":%d\"", $_; }
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
S/MIME Cryptographic Signature