I needed to get a list of strings representing time between two given PM times in specific intervals. So between 7pm and 8:20pm inclusive in 20 minute intervals you get: 7:00 7:20 7:40 8:00 8:20
I didn't use any Time libs becuase I found them slow for repetitive parsing plus i wanted to nut it out myself.
Just wondering what other ways people may go about this task while keeping the code fairly readable and not overly nested?
#!/usr/bin/perl
$t1 = '7:30'; #minimum time
$t2 = '11:45'; #maximum time
@t1 = split(':', $t1);
@t2 = split(':', $t2);
$interval = 15; #may be (10,15,20,30)
#create an array of intervals
for (0..(60/$interval)-1) {
$mins[@mins]=(($_*$interval) ? $_*$interval : '00');
}
#loop for number of hours requested
for ($t1[0]..$t2[0]) {
#loop for number of intervals in array
foreach $mins (@mins) {
#print if min <= result <= max
if ($_<=$t2[0]&&$mins<=$t2[1]) {
print "$_:$mins\n" if (!($_==$t1[0]&&$mins<$t1[1]))
}
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.