| Recent
Articles |
Controlling Concurrent Runs with Perl
Sometimes you have a program that can't be run by more than one person, or one
that must run frequently but you don't know for sure how long an instance of it
will take. One way to accomplish that is to use a lock file...
Beginning Web Development With Perl (Review)
While Perl may not be the "cool" language for websites anymore, there are some
of us who prefer to work with it because we use it for so many other tasks. I've
done PHP, and toyed with other things, but I always...
EMEA Ditching Perl, Python, PHP?
A spring 2005 survey of developers in Europe, the Middle East, and Africa finds
a dropoff in their use of those three languages. The June survey of 400 developers
by Evans Data Corporation revealed fewer developers using three common development
languages... ASP.NET
and IIS Serve More Fortune 1000 Sites Than any Other Web Server Technologies
A new survey of the 2005 Fortune 1000 Web sites reports that Microsoft's ASP.NET,
ASP, and Internet Information Services (IIS) serve the majority of leading U.S.
corporate sites...
Bit vector, using Perl vec
A bit vector is just an array of bits; subsets of bits within the bytes have some
meaning. That allows more compact storage for certain types of data. For example,
if you only needed boolean on-off values, you can store eight values in one byte....
A.P.
Lawrence On Pro Perl
In the review copy I read, there still were some unfortunate typos that might
confuse someone completely new to Perl. I hope those will be fixed before the
actual publication. None of them bothered me, but they could be bad for someone
starting with no experience at all... |
|
|
01.03.06
Rounding Time By
A.P. Lawrence
It was a long, hard day. Up early to clean a room of furniture so that new carpet
can be put in later this week, off to a real estate refinancing closing after
that, back to rake leaves and then the usual computer type work.
After dinner some refinishing of hardwood floors and a little sealing of cracks
noticed when I pulled up some of the old carpet. Before I went to bed, I checked
email and noticed something from a customer needing some help with rounding off
time.
Specifically, he had a string that might look like "DH154932" which represents
3:49 PM and some seconds. He needed that rounded off to the nearest previous 15
minutes. I looked at it, thought "that's not too hard", and then realized that
my head was about to fall onto the keyboard, so I went to bed. As I drifted off
I thought "floor() can do that", but I think my brain was confusing refinishing
the living room floor with rouding time, because it can't. But it is easy enough,
and when I woke up, I dashed off an email explaining how to do it.
The easiest way to handle this is to extract the minutes portion from the string
and round that down. But how do you round down to the previous fifteen minutes?
Well, you could do it the long way: $min=45
if $min > 45;
$min=30 if $min > 30;
$min=15 if $min > 15;
$min=0 if $min > 0;
And unless you have to process thousands of these each second, there's nothing
wrong with that. It's brute force, but so what? Far too often programmers employ
"clever" solutions when there is really no reason to. A straightforward method
like this is easy to understand. It may not be highly efficient, but if that doesn't
matter, why not use it? You'll never be confused by that code, and if you have
to modify it you'll have little danger of screwing it up.
But I don't know how much processing is going on here, so I gave him a different
method. It's probably faster, though I didn't actually test it and that's another
important point to remember: just because it looks faster doesn't mean it is.
If you really need efficiency, you have to test. The four "if" statements above
might actually run faster than a the single statement I used. Probably
not in this case; what I used was
$min=$min
- $min % 15;
which is quite direct and should compile into something pretty fast. But if I
was really worried, I'd set up tests.
So, the whole thing looks something like this:
#!/usr/bin/perl
# this.pl DH154951
$time=shift @ARGV;
$min=substr($time,4,2);
print "$time\n";
$min=$min - $min % 15;
$time=sprintf("%s%0.4d",substr($time,0,4),$min * 100);
print "$time\n";
As my tired brain thought, it wasn't very hard.
*Originally published at APLawrence.com
|