A Pack Statements
Introduction
The Pack statements provide a convenient means of transferring string and numeric data to and from a string variable. The UNPACK USING statement is particularly useful in conjunction with certain DBINFO modes, in which database information is returned in a string variable as a combination of ASCII characters and numeric integers.
Three Pack statements are available:
- PACKFMT
- Specifies the data format for a PACK USING or UNPACK USING statement.
- PACK USING
- Transfers data from variables in a pack list to a destination string.
- UNPACK USING
- Transfers data from a source string to variables in a pack list.
The PACKFMT Statement
PACKFMT pack list
The parameter is as follows:
- pack list
- A list of program variables, arrays, and/or skip fields separated by commas. This list contains an ordered set of variable names used by the PACK USING and UNPACK USING statements.
PACKFMT (pack format) defines a list of variables to be used in conjunction with a source or destination string referenced in an UNPACK USING or a PACK USING statement. Upon PACK USING execution, data is transferred from the PACK USING list variables to the destination string. Upon UNPACK USING execution, data is transferred from the source string to the pack list variables.
As the transfer occurs between the pack list variables and the string referenced in PACK USING or UNPACK USING, an internal pointer to the string's next position is updated. To skip character positions within the string, a skip indicator may be supplied in the appropriate position of the pack list (for example, 1X=one byte, 2X=two bytes). The PACK USING and UNPACK USING program examples, on the following pages, illustrate the use of the PACKFMT statement and the skip indicator.
The PACK USING Statement
PACK USING line id; destination string
The parameters are as follows:
- line id
- A line number or line label referencing the pack list.
- destination string
- A string variable that receives data contained in variables listed in a PACKFMT statement.
The PACK USING statement transfers data from each variable of the appropriate pack list to the destination string. The pack list is located on a separate program line. As the data is transferred to the destination string, its format is not altered in any way. Thus, a real-precision number requires eight bytes in the buffer; a short-precision number requires four bytes; and so on. When transferring a string from the pack list having a current length less than its dimensioned (or substring) length, the destination string is filled with blanks to equal the dimensioned (or substring) length.
The following example illustrates the use of the PACK USING statement:
10 INTEGER A
20 SHORT B
30 REAL C
40 DIM D$[10],E$[50]
50 A=47
60 B=89.5432
70 C=2.3456789
80 D$="IMAGE"
90 PACK USING Here;E$
100 END
110 Here: PACKFMT A,B,2X,D$,14X,C
After executing line 90, E$ contains the following:
Bytes | Value |
1-2 | Integer variable A |
3-6 | Short variable B |
7-8 | Skipped |
9-18 | String variable D$(Last 5 bytes are padded with spaces) |
19-32 | Skipped |
33-40 | Real variable C |
41-50 | Filled with spaces |
Example with User Defined Types:
10 DIM T AS Type
*
*
*
200 PACK USING Here;F$
*
*
230 Here: PACKFMT STRUCT T
The UNPACK USING Statement
UNPACK USING line id; source string
The parameters are as follows:
- line id
- A line number or line label referencing a PACKFMT statement.
- source string
- A string expression that contains data to be unpacked into variables listed in a PACKFMT statement.
Through the UNPACK USING statement, data is transferred from the source string to the variables appearing in the pack list. A one-to-one transfer is done without altering the data format. When transferring data to short- or real-precision variables, UNPACK USING verifies that the data qualifies as a valid numeric value.
The following example illustrates the use of the UNPACK USING statement:
10 DIM A$[12],F$[40]
20 INTEGER X1,X2
*
*
*
200 UNPACK USING Here;F$
210 DISP X1,A$,X2
220 END
230 Here: PACKFMT 4X,X1,A$,X2
After executing line 200, variables X1, X2 and A$ contain the following information:
Variable | Bytes in F$ |
X1 | 5-6 |
X2 | 19-20 |
A$ | 7-18 |
Example with User Defined Types:
10 DIM T AS Type
*
*
*
200 UNPACK USING Here;F$
210 PRINT STRUCT T
220 END
230 Here: PACKFMT STRUCT T
Eloquence Database Manual - 19 DEC 2002