For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2007 > printf problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printf problem
Julie A. Bolduc

2007-10-30, 7:59 am

I am trying to format some numbers so single digit numbers are converted to
2 digit numbers and here is the code I am working with right now. For some
reason, the very last number, $b5, ends up as a 3 digit number instead of a
2 digit number even if I start it with a single digit number. If I add
chop($bcol); to the code, it cuts the last digit off but what if I do not
need to have that happen? I am not exactly new to perl but this has me
stumped.


print "Content-type: text/html\n\n";
$b1=11;
$b2=2;
$b3=5;
$b4=1;
$b5=12;
$bcol=printf("%02d %02d %02d %02d %02d", $b1, $b2, $b3, $b4, $b5);

#chop($bcol);
print qq($bcol);

Can anyone help?


Julie A. Bolduc
Owner, JPF Crochet Club
http://www.jpfun.com

Andrew Curry

2007-10-30, 7:59 am

try using sprintf instead of printf.
i think the extra 1 you are getting on the end it just a true return.

-----Original Message-----
From: Julie A. Bolduc [mailto:jpfun@roadrunner.com]
Sent: 30 October 2007 00:23
To: beginners@perl.org
Subject: printf problem


I am trying to format some numbers so single digit numbers are converted to
2 digit numbers and here is the code I am working with right now. For some
reason, the very last number, $b5, ends up as a 3 digit number instead of a
2 digit number even if I start it with a single digit number. If I add
chop($bcol); to the code, it cuts the last digit off but what if I do not
need to have that happen? I am not exactly new to perl but this has me
stumped.


print "Content-type: text/html\n\n";
$b1=11;
$b2=2;
$b3=5;
$b4=1;
$b5=12;
$bcol=printf("%02d %02d %02d %02d %02d", $b1, $b2, $b3, $b4, $b5);

#chop($bcol);
print qq($bcol);

Can anyone help?


Julie A. Bolduc
Owner, JPF Crochet Club
http://www.jpfun.com


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/



This e-mail is from the PA Group. For more information, see
www.thepagroup.com.

This e-mail may contain confidential information. Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the sender
immediately. Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.

Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.




Paul Lalli

2007-10-30, 7:00 pm

On Oct 29, 8:22 pm, jp...@roadrunner.com (Julie A. Bolduc) wrote:
> I am trying to format some numbers so single digit numbers are converted to
> 2 digit numbers and here is the code I am working with right now. For some
> reason, the very last number, $b5, ends up as a 3 digit number instead of a
> 2 digit number even if I start it with a single digit number. If I add
> chop($bcol); to the code, it cuts the last digit off but what if I do not
> need to have that happen? I am not exactly new to perl but this has me
> stumped.
>
> print "Content-type: text/html\n\n";
> $b1=11;
> $b2=2;
> $b3=5;
> $b4=1;
> $b5=12;
> $bcol=printf("%02d %02d %02d %02d %02d", $b1, $b2, $b3, $b4, $b5);
>
> #chop($bcol);
> print qq($bcol);
>
> Can anyone help?


printf() prints the string to the screen, and returns a true value (1)
if successful.
sprintf() returns the string

You are printing to the screen and storing the result (the 1) in
$bcol, and then printing $bcol. Make up your mind. Either change the
printf() to sprintf() or eliminate $bcol entirely.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com