Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / Data Structures - some thoughts

Register 
新用户注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Data Structures - some thoughts
#13398
Posted by: PeterJ 2010-11-08 01:56:51 Last edited by: PeterJ 2010-11-08 02:06:52 (Total edited 3 times)
If you process files with structured records, you need to extract parts of it and maybe convert it and assign it rex variables. Creating records must work in the opposite direction. For Extracting variables out of a record a series of SUBSTR functions is used, to push variables into a record we use OVERLAYs. 

I created a tiny script which might be useful.

The structure is defined in a compound variable (Stem) and has an entry for each data item:

structure.1="Name      Char 10"   /* variable Name type is Character, length 10             */
structure.2="IDNo      Bin   2"   /* variable IDNO type is Binary, length 2 (Bytes)         */
structure.3="Telephone NUM   8"   /* variable Telephone type is Numeric, length 8 (Bytes)   */
structure.4="Address   VAR   *"   /* variable Address type is VAR(CHAR), length is variable */
structure.0=4 
first the variable name is defined, followed by the type and length, the offset in the record is calculated internally.
Currently there are the following types:
      CHAR    string of certain length
      NUM     numeric string (as CHAR) but it will be "right" justified
      BIN       binary number, the numeric value is translated into its binary representation and vice versa.
      VAR     Character string with variable length, therefore you don't need the length
      DEC     packed fields (for our hosties) will be packed and unpacked

The definition will be passed to the STRUCTURE procedure in DEFINE mode. The necessary statements to push the variables in a record and fetch it from a record will be created.  Later on these statements will be used as "Interpret" Macros.        

Check out the following example to understand the method:

/* -----------------------------------------------------------------------------------
 * 1. Let's define a structure 
 * -----------------------------------------------------------------------------------
 */
structure.1="Name      Char 10"   /* variable Name type is Character, length 10             */
structure.2="IDNo      Bin   2"   /* variable IDNO type is Binary, length 2 (Bytes)         */
structure.3="Telephone NUM   8"   /* variable Telephone type is Numeric, length 8 (Bytes)   */
structure.4="Address   VAR   *"   /* variable Address type is VAR(CHAR), length is variable */
structure.0=4 
/* Now the structure gets contructed                   */ 
structure('DEFINE',structure.)
SAY "1. Structure defined"
/* Set Variables, which will become part of the record */ 
NAME="John Smith"
idno="2178"
telephone=442518234
ADDRESS="Leicester Court Road 2356"
SAY "2. Variables set"
/* Let's build the record                              */ 
record=structure('PUT',structure.)
SAY "3. compose record from Variables"
/* drop name, as we want to rebuild the variables contents out of the record */
DROP NAME idno telephone ADDRESS 
SAY "4. Variables dropped, as we fetch them back from record"
/* extract the variables from record, and print them */
SAY "5. Fetch Variables from record" 
structure('GET',structure.,record)
SAY 'Name     : 'NAME
SAY 'IDNo     : 'idno
SAY 'Telephone: 'telephone
SAY 'Address  : 'ADDRESS
SAY "6. Variables created using structure definition from record" 
EXIT
If you read an process a file you need the STRUCTURE(GET,...) method.
To write records STRUCTURE(PUT,...). Reading and writing records is not part of the functions, and must be performed with appropriate commands.
The structure procedures are attached!
Structure.rex
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Fri 2017-9-22  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0