270 likes | 566 Views
Boston University. 2. Date/time masks. DD Day. MM Month. YYYY Year, 4 digits. YY Year, 2 digits. WW Number of Week.JJJJulian day. N(n) Name of Day (language-dependent). ONumber of week day L(n) Name of Month (language-dependent). R Year in Roman numerals (maximum 13 digits). . Bo
E N D
1. Boston University 1 Natural 4 FundamentalsPart II – Manipulating Natural data Boston University at a Glance
4th largest educational and research institution
30,000 under-graduate, graduate and other students
17 schools and colleges across two campuses
1 hospital - Boston University Medical Center
3,321 full/part time faculty members
8,538 full/part time employees
1.19 billion dollar annual operating budget
930.2 million dollar endowment
52 developers and support staff in applications
3 application and 4 technical DBA’s
3 DBMS - DB2, ADABAS, and Oracle
* data as of April 2001
Boston University at a Glance
4th largest educational and research institution
30,000 under-graduate, graduate and other students
17 schools and colleges across two campuses
1 hospital - Boston University Medical Center
3,321 full/part time faculty members
8,538 full/part time employees
1.19 billion dollar annual operating budget
930.2 million dollar endowment
52 developers and support staff in applications
3 application and 4 technical DBA’s
3 DBMS - DB2, ADABAS, and Oracle
* data as of April 2001
2. Boston University 2 Date/time masks DD Day.
MM Month.
YYYY Year, 4 digits.YY Year, 2 digits.
WW Number of Week.
JJJ Julian day. N(n) Name of Day (language-dependent).
O Number of week day L(n) Name of Month (language-dependent).
R Year in Roman numerals (maximum 13 digits).
3. Boston University 3 Affect of DTFORM setting The profile parameter DTFORM determines
whether Monday or Sunday is considered the first
day of the week.
With DTFORM=U :
Sunday = 1, Monday = 2, …
With DTFORM=other :
Monday = 1, Tuesday = 2, …
This has an affect on the EM=O
4. Boston University 4 Date Checking Several customers utilize code similar to the following and have requested a written explanation of the difference between the valid dates for MASK statements versus MOVE EDITED statements.
RESET field1(A8) field2(D)
INPUT field1
IF field1 = MASK(YYYY) THEN
MOVE EDITED field1 TO field2 (EM=YYYY)
ELSE
DISPLAY 'error in date field'
END-IF
As documented in the NATURAL Reference Manual, the MASK (YYYY) checks for a valid date between 0000 and 2699. The MOVE EDITED statement, however, checks for a valid Gregorian date. Keep in mind the Gregorian year begins in 1582; therefore, the date in the MOVE EDITED statement must begin with the year 1582 or greater. If the date is not 1582 or greater, you will receive a NAT1143 error.
5. Boston University 5 Date Checking In order to avoid receiving the NAT1143 error, there
are three possible coding methods:
Write a check in the program (or processing rule) for YEAR >=1582 and YEAR <=2699.
Add an ON ERROR routine which captures the NAT1143 error and issues back to the end-user a 'friendly' message stating the year (on the MOVE EDITED statement) must begin with the year 1582 or greater.
Place text on the INPUT statement stating that the year to beinput must begin with the year 1582 or greater.
6. Boston University 6 Mask Testing MASK (N) - F0 thru F9
MASK (Z) - 'correct' hex character in last nibble position - 'C', 'F', (positive) 'D' (negative)
MASK (D) - legal date according to DTFORM (use *DATX)
MASK (T) - legal time according to DTFORM
MASK (E) - legal date & time
MASK (YYYY) – checks valid 4-char. Year
MASK (C) – checks for alphanumeric
MASK (S) – checks special characters
MASK (U) – checks upper case
MASK (L) – checks lower case Testing
1. IF FIELD = MASK …
2. The IS operator
IF FIELD IS (N4) or
IF FIELD IS (D)
3. IF #CV MODIFIED Testing
1. IF FIELD = MASK …
2. The IS operator
IF FIELD IS (N4) or
IF FIELD IS (D)
3. IF #CV MODIFIED
7. Boston University 7 Example of interesting use of MASK(S)
How do you determine if an alphanumeric field
contains a special character?
Natural provides a test for a special character, but
surprises are in order !
1 #EXCL (A1) CONST <'!'>
*
IF #EXCL = MASK (S)
WRITE 'AN EXCLAMATION IS A SPECIAL CHARACTER.'
ELSE
WRITE 'AN EXCLAMATION IS NOT A SPECIAL CHARACTER TO NATURAL.'
END-IF DEFINE DATA LOCAL
1 #CHARS(A1/25) INIT (V) <'!','@','#','$','%','¬','&','*','(',')',’-‘,’_’,’+','<',
'>','?','/','\','{','}','~','`','=','˘','|'>
1 #I (P3)
1 #EXCL (A1) CONST <'!'>
END-DEFINE
*
IF #EXCL = MASK (S)
WRITE 'AN EXCLAMATION IS A SPECIAL CHARACTER.'
ELSE
WRITE 'AN EXCLAMATION IS NOT A SPECIAL CHARACTER TO NATURAL.'
END-IF
*
FOR #I = 1 25
IF #CHARS(#I) = MASK (S)
WRITE #I #CHARS(#I) 'IS A SPECIAL CHARACTER'
ELSE
WRITE #I #CHARS(#I) 'IS NOT A SPECIAL CHARACTER.'
END-IF
END-FOR
ENDDEFINE DATA LOCAL
1 #CHARS(A1/25) INIT (V) <'!','@','#','$','%','¬','&','*','(',')',’-‘,’_’,’+','<',
'>','?','/','\','{','}','~','`','=','˘','|'>
1 #I (P3)
1 #EXCL (A1) CONST <'!'>
END-DEFINE
*
IF #EXCL = MASK (S)
WRITE 'AN EXCLAMATION IS A SPECIAL CHARACTER.'
ELSE
WRITE 'AN EXCLAMATION IS NOT A SPECIAL CHARACTER TO NATURAL.'
END-IF
*
FOR #I = 1 25
IF #CHARS(#I) = MASK (S)
WRITE #I #CHARS(#I) 'IS A SPECIAL CHARACTER'
ELSE
WRITE #I #CHARS(#I) 'IS NOT A SPECIAL CHARACTER.'
END-IF
END-FOR
END
8. Boston University 8 Questionable Use of IS
9. Boston University 9 Suggestion to add… Consider adding …
DEFINE DATA LOCAL
1 #F (A10)
1 REDEFINE #F
2 #F2 (N10)
1 #N (P10)
END-DEFINE
*
ASSIGN #F = ‘123453789'
IF #F IS (N10)
#N := VAL(#F)
WRITE ‘NUMERIC VALUE TO USE.’ #N
ELSE
WRITE 'DATA FIELD ERROR, DO NOT USE IT’ #F
END-IF
END
10. Boston University 10 How to Test for Packed? 1). Always use the Packed field name in the MASK statement.
2). Do NOT use the name of an Alpha field which is either a redefinition of the packed field, or the field the Packed field redefines.
3). In the MASK, specify the total number of digits the Packed field can contain, making the last one a "Z" in the MASK.
A violation of the above three points will result in an incorrect result.
11. Boston University 11 Date mask problem The MASK (YYYY) checks for a valid date between 0000 and 2699. The MOVE EDITED statement checks for a valid Gregorian date, which begins in 1582. Therefore, the date in the MOVE EDITED statement must begin with the year 1582 or greater. If the date is not 1582 or greater, you will receive a NAT1143 error.
12. Boston University 12 Avoiding NAT1143 Three possible coding methods:
Write a check in the program (or processing rule) for YEAR >=1582 and YEAR <=2699.
Add an ON ERROR routine which captures the NAT1143 error and issues back to the end-user a 'friendly' message stating the year (on the MOVE EDITED statement) must begin with the year 1582 or greater.
Place text on the INPUT statement stating that the year to be input must begin with the year 1582 or greater.
13. Boston University 13 Array definitions Arrays are fundamentally data elements arranged in
tables; elements in a table are represented by a single
variable name.
You can define up to 3 dimensions. Higher level
dimensions can be simulated.
Quiz: What is the meaning of:
DEFINE DATA LOCAL
1 #ROW (A5/4)
1 #COLUMN (A10/2:8)
END-DEFINE What [if any] is the difference between these two definitions?
DEFINE DATA LOCAL
1 PRJT-T (1:10)
2 PROJ-ID (A4)
2 USER-T (1:10)
3 USER-ID (A6)
3 FUNC-T (1:10)
4 FUNC-CD (A2)
END-DEFINE
and
DEFINE DATA LOCAL
1 PRJT-T (1:200)
2 PROJ-ID (A4)
2 USER-ID (A6)
2 FUNC-CD (A2)
END-DEFINE What [if any] is the difference between these two definitions?
DEFINE DATA LOCAL
1 PRJT-T (1:10)
2 PROJ-ID (A4)
2 USER-T (1:10)
3 USER-ID (A6)
3 FUNC-T (1:10)
4 FUNC-CD (A2)
END-DEFINE
and
DEFINE DATA LOCAL
1 PRJT-T (1:200)
2 PROJ-ID (A4)
2 USER-ID (A6)
2 FUNC-CD (A2)
END-DEFINE
14. Boston University 14 Arrays definitionscontinued… Database arrays
M 2 AG ADDRESS-LINE A 20 N
:
P 1 AQ INCOME
2 AR CURR-CODE A 3 N
HD=CURRENCY/CODE
2 AS SALARY P 9.0 N
HD=ANNUAL/SALARY
M 2 AT BONUS P 9.0 N
For MU inside Pes, an example of coding for occurrences is:
1 STAFF VIEW OF EMPLOYEES
2 PERSONNEL-ID
2 NAME
2 SALARY (1:6)
2 CURR-CODE (1:6)
2 BONUS (1:6,1:6) How does one decide to use ‘P’ vs. ‘N’ format for numeric values in a DDM? Is there any difference?How does one decide to use ‘P’ vs. ‘N’ format for numeric values in a DDM? Is there any difference?
15. Boston University 15 A consideration of a performance issue for Adabas access Database packed definitions on DDM view
1 AA STAFF-ID A 10 N
:
1 AQ PERSONAL-DATA
2 AR STATE-CODE A 4 N
2 AS BLDG-ID N 5.0 N
2 AT DATE-OF-BIRTH N 8.0 N
2 AU OVERDUE-LIBR-BOOK-QTY P 3.0 N
2 AV SALARY-AMT P 8.0 N
16. Boston University 16 Some performance considerations Do integer arithmetic with I definitions first, P second. Also good for cross platform formatting.
Keep literal assignments and variables the same length!
Use EXAMINE on arrays vs. FOR loops wherever possible
For complex computations, normalize data element (type and length). Avoid mix mode expressions.
RESET instead of MOVE
17. Boston University 17 Arithmetic operations Simple - use integer or packed for
arithmetic, and there is no
difference in performance between
Formula computations and individual
arithmetic statements whose sum
performance is equivalent to a
formula.
The packed notion becomes more suspect if you have cross platform Natural requirements. Packed is restricted to S/390 instruction machines.The packed notion becomes more suspect if you have cross platform Natural requirements. Packed is restricted to S/390 instruction machines.
18. Boston University 18 Natural Math. Functions Operation Function Comment
Addition +
Subtraction -
Multiplication *
Division /
Exponentiation ** integer exponents only
Square root SQRT operand cannot be an expression, NATURAL evaluates negative operands as positive Use of square root function:
* SUBPROGRAM: STNDDEV
* FUNCTION: COMPUTE STANDARD DEVIATION
* ----------------------------------------------------------
* ALGORITHM: STAND. DEV. = SQUARE ROOT (VARIANCE)
* VARIANCE = SUM OF SQUARE OF DIFFERENCES BETWEEN
* EACH DATUM AND AVERAGE
* ----------------------------------------------------------
* EJECT
DEFINE DATA PARAMETER
1 #ARR-ELT (P8.3/1:V) /* INPUT
1 #ARRAY-LENGTH (I4) /* INPUT
1 #STND-DEV (P10.3) /* OUTPUT
1 #VARIANCE (P10.3) /* OUTPUT
LOCAL
1 #AVERAGE (P10.3)
1 #NUM (P8.3)
1 #TOTAL (P12.3)
1 #I (I4)
END-DEFINE
* SKIP 1
#ARRAY-LENGTH := *OCCURRENCE (#ARR-ELT)
FOR #I = 1 TO #ARRAY-LENGTH
#TOTAL := #TOTAL + #ARR-ELT (#I)
END-FOR
* SKIP 1
COMPUTE #AVERAGE = #TOTAL / #ARRAY-LENGTH
*
FOR #I = 1 TO #ARRAY-LENGTH
#NUM := #ARR-ELT (#I)
#TOTAL := #TOTAL + (#AVERAGE - #NUM) ** 2
END-FOR
* SKIP 1
#VARIANCE := #TOTAL / #ARRAY-LENGTH
#STND-DEV := SQRT (#VARIANCE)
ESCAPE ROUTINE
ENDUse of square root function:
* SUBPROGRAM: STNDDEV
* FUNCTION: COMPUTE STANDARD DEVIATION
* ----------------------------------------------------------
* ALGORITHM: STAND. DEV. = SQUARE ROOT (VARIANCE)
* VARIANCE = SUM OF SQUARE OF DIFFERENCES BETWEEN
* EACH DATUM AND AVERAGE
* ----------------------------------------------------------
* EJECT
DEFINE DATA PARAMETER
1 #ARR-ELT (P8.3/1:V) /* INPUT
1 #ARRAY-LENGTH (I4) /* INPUT
1 #STND-DEV (P10.3) /* OUTPUT
1 #VARIANCE (P10.3) /* OUTPUT
LOCAL
1 #AVERAGE (P10.3)
1 #NUM (P8.3)
1 #TOTAL (P12.3)
1 #I (I4)
END-DEFINE
* SKIP 1
#ARRAY-LENGTH := *OCCURRENCE (#ARR-ELT)
FOR #I = 1 TO #ARRAY-LENGTH
#TOTAL := #TOTAL + #ARR-ELT (#I)
END-FOR
* SKIP 1
COMPUTE #AVERAGE = #TOTAL / #ARRAY-LENGTH
*
FOR #I = 1 TO #ARRAY-LENGTH
#NUM := #ARR-ELT (#I)
#TOTAL := #TOTAL + (#AVERAGE - #NUM) ** 2
END-FOR
* SKIP 1
#VARIANCE := #TOTAL / #ARRAY-LENGTH
#STND-DEV := SQRT (#VARIANCE)
ESCAPE ROUTINE
END
19. Boston University 19 Natural Math. Functionscontinued… Absolute value ABS (X)
Arctangent ATN (X)
Cosine COS (X)
Exponential function EXP (X)
Fractional part of field FRAC (X)
Integer part of field INT (X)
NATURAL log (base e) LOG (X)
Sign of field SGN (X)
returned values are -1,0,+1
Sine SIN (X)
Tangent TAN (X)
Extract num. value from alpha VAL (X)
Returns B4 return code RET (X) 'X' is a user program, invoked via CALL
Internal unique field rep. POS (X) 'X' is a field name
1. For those not familiar with the exponential function, it returns a value in base e. For example, #Y = EXP(#X), where #X = 2, returns a value of 7.38905 because it is approximately e, which the Calculus teaches us is approximately 2.71828, to the power of 2.
2. Currently, none of the functions can be referenced in a WRITE/DISPLAY statement without getting a NAT0044 error. Also, it is documented that the format and length of the resultant takes on the characteristics of the operand to the function. I do not believe this fact is documented correctly. In my attempt to conclude if the trig functions took radian or degree measure, I wrote the following example:
0010 #X (P4) = 45
0020 #Y (P2.6) = SIN (#X)
0030 WRITE #Y
0040 END
If the documentation were correct, I would expect the result to be of format/length P4, but in fact the result was recorded with 6 position fractional accuracy. This implies that the result is actually the “largest and most accurate” of any operand in the compute statement.
1. For those not familiar with the exponential function, it returns a value in base e. For example, #Y = EXP(#X), where #X = 2, returns a value of 7.38905 because it is approximately e, which the Calculus teaches us is approximately 2.71828, to the power of 2.
2. Currently, none of the functions can be referenced in a WRITE/DISPLAY statement without getting a NAT0044 error. Also, it is documented that the format and length of the resultant takes on the characteristics of the operand to the function. I do not believe this fact is documented correctly. In my attempt to conclude if the trig functions took radian or degree measure, I wrote the following example:
0010 #X (P4) = 45
0020 #Y (P2.6) = SIN (#X)
0030 WRITE #Y
0040 END
If the documentation were correct, I would expect the result to be of format/length P4, but in fact the result was recorded with 6 position fractional accuracy. This implies that the result is actually the “largest and most accurate” of any operand in the compute statement.
20. Boston University 20 Notes on Math. Functions Lastly, the trig functions take the values of its
operands as radian measure, not degree
measure. The ”best” conversion formula to use
in NATURAL is
RAD = PI * DEG / 180.0
where PI is defined as a CONST of value 3.14157, DEG is defined to contain the degree measure (e.g., P4 format).
NATURAL logs can be converted to common logs (base 10) using the simple formula:
Common LOG(X) = NATURAL LOG (X) / NATURAL LOG 10
21. Boston University 21 Two important array operations Sorting arrays - See example
Searching arrays
22. Boston University 22 Search – Use EXAMINE DEFINE DATA LOCAL
1 #PGM-ARRAY (A8/500)
1 #INDEX (P4)
1 #SEARCH-FIELD (A8)
END-DEFINE
#SEARCH-FIELD = 'TAL0530N'
EXAMINE #PGM-ARRAY(*) FOR #SEARCH-FIELD
GIVING POSITION #INDEX BEWARE!!
There is one issue for which care must be taken in searching lists. If the search string is shorter than the occurrences, the wrong EXAMINE statement can return unexpected results. You must use an EXAMINE statement with the FULL option for both the search field and the array to guarantee returning the correct count. BEWARE!!
There is one issue for which care must be taken in searching lists. If the search string is shorter than the occurrences, the wrong EXAMINE statement can return unexpected results. You must use an EXAMINE statement with the FULL option for both the search field and the array to guarantee returning the correct count.
23. Boston University 23 Date Arithmetic Operation Result
1. (D) + (D) not allowed
2. (T) + (T) (T)
3. (D) + integer (D)
4. (T) + integer (T)
5. (D) + (T) (T)
6. (D) – (D) integer, in days
7. (T) – (T) integer, in tenths of seconds
8. (D) – integer (D)
9. (T) – integer (T)
10. (D) – (T) integer, in tenths of seconds
11. (T) – (D) integer, in tenths of seconds Integer values can be of formats N, P, or I.
Integer values can be of formats N, P, or I.
24. Boston University 24 Date arithmeticcontinued… 0010 DEFINE DATA LOCAL
0020 1 #TIME (T)
0030 1 #DATE (D)
0040 1 #HOUR (P8) INIT <36000> /* TENTHS SECONDS IN 1 HOUR
0050 END-DEFINE
0060 *
0070 MOVE *TIMX TO #DATE
0080 MOVE *TIMX TO #TIME
0090 WRITE 'CURRENT DATE / TIME ' '=' #DATE '=' #TIME
0100 *
0110 ADD ( 36 * #HOUR ) TO #TIME
0120 MOVE #TIME TO #DATE
0130 WRITE 'DATE / TIME 36 HOURS PLUS ' '=' #DATE '=' #TIME
0140 *
0150 END
How would you define a second?
01 #SECOND (P8) INIT <10> How would you define a second?
01 #SECOND (P8) INIT <10>
25. Boston University 25 Date example
0010 DEFINE DATA LOCAL
0020 1 #TODAY (D)
0030 1 #FUTURE-DATE (D)
0040 1 #FUTURE-GREG-DATE (A8) INIT <‘09-10-30'>
0050 1 #CURRENT-DATE (A8)
0060 1 #DAYS-DIFFERENTIAL (P4)
0070 END-DEFINE
0080 #TODAY = *DATX
0090 MOVE EDITED #FUTURE-GREG-DATE TO
0100 #FUTURE-DATE (EM=YY'-'MM'-'DD)
0110 MOVE EDITED #TODAY (EM=YY'-'MM'-'DD) TO #CURRENT-DATE
0120 #DAYS-DIFFERENTIAL = #FUTURE-DATE - #TODAY
0130 WRITE 'THE NUMBER OF DAYS BETWEEN' #FUTURE-GREG-DATE
0140 'AND TODAY' #TODAY 'IS' #DAYS-DIFFERENTIAL
0150 END
26. Boston University 26 Quiz DEFINE DATA LOCAL
1 #T (T)
END-DEFINE
*
#T := *TIMX
IF #T = D'2006-10-13' THRU D'2006-10-15' /* Friday thru Sunday
WRITE 'WITHIN DATE RANGE'
ELSE
WRITE 'NOT WITHIN DATE RANGE'
END-IF
END
Why does the execution at 1:00 AM on the morning of October 15 would the result be:
NOT WITHIN DATE RANGE
27. Boston University 27 Quiz DEFINE DATA LOCAL
1 #DAY (A10)
1 #MONTH (A10)
1 #D (D)
END-DEFINE
#D := *DATX
MOVE EDITED #D (EM=N(9)) TO #DAY
What does this do?
End of session. Questions? Comments?
28. Boston University 28 Last Quiz question !
How can you tell how much data
space is available to your
session and how much you are
using with execution?