Hi,
There are a dozen ways it could be done,
but I chose to use a pretty one-liner: Quarter=%div(Month:4) + 1;
As Simon already demonstrated, there are some problems with your one-liner,
but
why to reinvent the wheel, if there is already a SQL scalar function, that
calculates the quarter from a given date?
Exec SQL Set :MyQuarter = Quarter(:MyDate);
@Scott:
Here some (simple) tips:
Before reinventing the wheel or searching through CEE and other APIs, have a
look at the SQL scalar functions, if there is one that resolves your
problem. BTW RPG has currently around 75 Built-in-Functions. SQL offers
around 130 scalar functions.
Scalar functions can be used in RPG with out accessing a file, simply by
using a SET statement (which can be compared with the Opcode EVAL in RPG).
Examples:
Exec SQL Set :WeekDayNum = DayOfWeek_ISO(:MyDate); //Get the numeric Day
of Week (Monday=1)
Exec SQL Set :MyMonthName = DayName(:MyDate); //Get the name of month
Exec SQL Set :WeekISO = Week_ISO(:MyDate); //Get the numeric week
according to ISO guidelines
Exec SQL Set :MyString = Upper(:MyString); //Convert text to upper
case
Exec SQL Set :MyString = Replace(:MyString: '-': ''); //Remove all
dashes from a string
Exec SQL Set :MyNum = Round(:MyNumOld: -3); //Round to full 1,000
Exec SQL Set :MyNum = Ceiling(:MyNumOld); //Round up to the next
integer
... And so on
If you don't want to use these scalar functions, you may write wrapper
(RPG-)functions around.
Next tip:
An other thing I often see is and which is quite simple, but apperently
(almost) not known:
Looping through arrays even if it is not neccessary:
Assumed we have 4 Arrays with 12 elements (1 for each month), representing:
Sales, Charges, Profit, VAT
To calculate the profit, for all 12 months or the VAT, no loop is necessary:
Profit = Sales - Charges;
VAT = Sales * 0.19;
An other tip:
Delete modules after binding.
A module is necessary to let the language compiler run over the source code
and convert it into a peace of excutable code.
But a module must be bound in either a program or service program.
After binding (to either a program or service program) the module is no
longer needed and can be deleted.
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
"Shoot for the moon, even if you miss, you'll land among the stars." (Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them
and keeping them!"
-----Ursprüngliche Nachricht-----
Von: rpg400-l-bounces@xxxxxxxxxxxx [mailto:rpg400-l-bounces@xxxxxxxxxxxx] Im
Auftrag von Joe D
Gesendet: Monday, March 31, 2008 02:17
An: RPG programming on the AS400 / iSeries
Betreff: Re: Classic Traps -- I need your input!
----- Original Message -----
From: "Scott Klement" <rpg400-l@xxxxxxxxxxxxxxxx>
To: "RPG programming on the AS400 / iSeries" <rpg400-l@xxxxxxxxxxxx>
Sent: Sunday, March 30, 2008 3:48 PM
Subject: Classic Traps -- I need your input!
Folks, I'm working on an article that's tentatively titled "Classic
Traps and How to Avoid Them". The general idea is to discuss the
classic mistakes that programmers make while programming, and (where
necessary) explain how to avoid that.
I would like to offer up two very related traps:
1 - Some programmers are very quick to jump to the decision to rewrite a
program or module from scratch when all that is needed is a simple fix. If
the original code is ugly, confusing, or simply outdated, these programmers
chose to reinvent the wheel rather than make the simple fix. Their tendency
to always rewrite often makes even seeing the simple fix more difficult.
2 - Other programmers are very hesitant to ever rewrite a program or module.
If the original code is ugly, confusing, or simply outdated, these
programmers often invest far more time attempting to retrofit a fix or
enhancement to the existing program than it would take to start from
scratch. Their tendency to never rewrite often makes even seeing the simple
fix more difficult.
"Always Rewriters" and "Never Rewriters" are very different programmers, but
you would be surprised how many people I have met at one extreme or the
other. You would be equally surprised by how few people I have seen whose
tendencies lie anywhere near the middle of the two.
As for How To Avoid, I have always tried to teach programmers at one extreme
or another to document why the other approach is not valid. Teaching
someone to have an open mind is not a trivial task, but when someone is
asked to justify their decisions they tend to at least consider the other
approach instead of blindly pushing forward.
FYI - I like to think I am near the middle of the two extremes, but in truth
I think I definitely lean toward the Always Rewrite side. When I come
across ugly code I find it hard not to replace it with something prettier or
more efficient. For example, yesterday I replaced 30+ lines of code that
determined the 'Quarter Number' for a given month. The routine worked just
fine, but it still bothered me... There are a dozen ways it could be done,
but I chose to use a pretty one-liner: Quarter=%div(Month:4) + 1;
Good luck with the article,
JD
As an Amazon Associate we earn from qualifying purchases.