On 2/9/11 Wed  Feb 9, 2011  2:30 PM, "Mike Blezien"
<mick...@frontiernet.net> scribbled:

> ----- Original Message -----
> From: "Jim Gibson" <jimsgib...@gmail.com>
> To: "Perl List" <beginners@perl.org>
> Sent: Wednesday, February 09, 2011 4:04 PM
> Subject: Re: Randomizing a 24hr time period
> 
> 
>> On 2/9/11 Wed  Feb 9, 2011  1:05 PM, "Mike Blezien"
>> <mick...@frontiernet.net> scribbled:
>> 
>>> ----- Original Message -----
>>> From: "Paul Johnson" <p...@pjcj.net>

>>> 
>>> Paul,
>>> 
>>> quick question. my perl is a bit rusty, been away from it for awhile. But
>>> this
>>> line in your coding:
>>> 
>>> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for @times;
>>> 
>>> I need to put the code:   %02d:%02d    into a variable to store it in a
>>> database, the hour:minute, how would I go about putting them into a
>>> variable?
>> 
>> "%02:%02d" is not code as such. It is part of the format specifier for the
>> printf function.
>> 
>> You can use a string variable for the format specifier:
>> 
>> my $fmt = "%02d:%02d";
>> print $fmt, $hour, $min;
>> 
>> If that is not what you want, then maybe you want to put the string that is
>> printed according to the specified format into a variable instead of writing
>> to an output stream. To do that, use the sprintf function instead of printf:
>> 
>> my $hhmm = sprintf("%02:%02",$hour,$min);
> 
> Ok how would I go about getting this variable from this line or I'm I missing
> something:
> 
> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for @times;
> 
> instead of printing it output ? This where I get messed up.

That statement is a little complex for a beginner's list (IMO), as is the
rest of Paul's program.

That one line above is equivalent to the following:

my $n;
for ( @times ) {
  printf( "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60);
}

You are only interested in the hour:minute part of the output, so you can
ignore the counter $n, and for multi-line loops you should use an explicit
loop variable instead of the default $_, giving:

for my $hr ( @times ) {
  my $hhmm = sprintf( "%02d:%02d", $hr, ($hr - int $hr) * 60);
  # do something with $hhmm
}

The @times array contains numerical values in hours, so $hhmm will contain a
string of the form "23:59", depending upon the values entered for the number
of messages and the duration.



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


Reply via email to