Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Basic programming question

by sleepingsquirrel (Hermit)
on Jan 27, 2005 at 16:32 UTC ( #425608=note: print w/ replies, xml ) Need Help??


in reply to Basic programming question

Perl has a special conditional operator (the ternary ?:) which is custom made for just this type of thing.

my $T = ($hour > 12) ? "PM" : "AM";
update: Whoops, just noticed that the OP is also decreasing $hour for the "PM" case. In that case, the ternary operator becomes a little more ugly.
my $T = ($hour > 12) ? do {$hour-=12; "PM"} : "AM";
Your best bet is probably one of the CPAN modules like dragonchild recommends.


-- All code is 100% tested and functional unless otherwise noted.


Comment on Re: Basic programming question
Select or Download Code
Replies are listed 'Best First'.
Re^2: Basic programming question
by tall_man (Parson) on Jan 27, 2005 at 16:44 UTC
    You missed subracting from the hour with this substitution. Update: The update fixes the problem, but I still prefer the simpler options like B for readability.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://425608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2015-07-29 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls