! string with format HH:MM:SS !

A

Alextophi

hi


I have a problem to format string with format HH:MM:SS, the chains
$hour, $minut and $second are calculated data.

my $hour; #0-23
my $minut; #0-59
my $second; #0-59

print "$hour:$minut:$second\n";

return
ex: 1:20:6

and I will want
ex: 01:20:06


Tank's
christophe
 
B

Brian Wakem

Alextophi said:
hi


I have a problem to format string with format HH:MM:SS, the chains
$hour, $minut and $second are calculated data.

my $hour; #0-23
my $minut; #0-59
my $second; #0-59

print "$hour:$minut:$second\n";

return
ex: 1:20:6

and I will want
ex: 01:20:06


Tank's
christophe



perldoc -f sprintf
 
B

Brian Wakem

Brian said:
perldoc -f sprintf


I've got time for a longer answer now. You actually want printf, but
perldoc -f printf wont tell you how to use the formatting, it refers you
to sprintf.
 
W

wisefamily

Alextophi said:
I have a problem to format string with format HH:MM:SS, the chains
$hour, $minut and $second are calculated data.

my $hour; #0-23
my $minut; #0-59
my $second; #0-59

print "$hour:$minut:$second\n";

return
ex: 1:20:6

and I will want
ex: 01:20:06

To do that, you should look up the documentation for printf or sprintf,
but the way you do that is:

printf('%02d:%02d:%02d', $hour, $minut, $second);

or, if you want to get that into a string, you do it with the sprintf
function:

my $formatted = sprintf('%02d:%02d:%02d', $houd, $minut, $second);
print $formatted;

Hope this helps,
David
 
E

Eric J. Roode

I have a problem to format string with format HH:MM:SS, the chains
$hour, $minut and $second are calculated data.

my $hour; #0-23
my $minut; #0-59
my $second; #0-59

print "$hour:$minut:$second\n";

return
ex: 1:20:6

and I will want
ex: 01:20:06

Two possibilities:

1: printf "%02d:%02d:%02d\n", $hour, $minute, $second;

2: use Time::Normalize;
($hour, $minute, $second) = normalize_hms ($hour, $minute, $second);
print "$hour:$minute:$second\n";

The latter will also bounds-check your values.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top