Progress
Programming
Handbook


Using the IMPORT Statement

The IMPORT statement is the counterpart of the EXPORT statement. It reads an input file into Progress procedures, one line at a time.

Using IMPORT to Read Standard Data

The following example shows IMPORT reading the file exported by the procedure p-export.p:

p-import.p
INPUT FROM p-datfl6.d. 
REPEAT: 
   CREATE customer. 
   IMPORT cust-num name sales-rep. 
END. 
INPUT CLOSE. 

This relies on the input being space separated. You can also use the DELIMITER option of the IMPORT statement to read a file with a different separator.

For example, p-imprt2.p reads the file produced by p-exprt2.p in the previous section:

p-imprt2.p
INPUT FROM p-datfl7.d. 
REPEAT: 
   CREATE customer. 
   IMPORT DELIMITER "," cust-num name sales-rep. 
END. 
OUTPUT CLOSE. 

This example reads one line at a time from p-datfl7.d into the character-string variable data. It then breaks the line into discrete values and assigns them to the fields of a customer record.

Using IMPORT to Read Non-standard Data

Although the IMPORT statement is used primarily to read data in the standard format written by the EXPORT statement. However, you can use the UNFORMATTED and DELIMITER options of IMPORT to read data in non-standard formats.

When you use the UNFORMATTED option, the IMPORT statement reads one line from the input file. For example, suppose your input file is formatted as follows:

p-datf12.d
90 
Wind Chill Hockey 
BBB 
91 
Low Key Checkers 
DKP 
92 
Bing’s Ping Pong 
SLS 

The lines containing cust–num and sales–rep values can be read with normal IMPORT statements. However, if you try to read the customer name values with a normal IMPORT statement, only the first word of each name is read—the space character is treated as a delimiter. To prevent this, read the name with the UNFORMATTED option, as in p-impun1.p.

p-impun1.p
INPUT FROM p-datfl2.d. 
REPEAT: 
   CREATE customer. 
   IMPORT customer.cust-num. 
   IMPORT UNFORMATTED customer.name. 
   IMPORT customer.sales-rep. 
END. 
INPUT CLOSE. 

Now, suppose each line of the file contained a cust–num, name, and sales–rep value, but no special delimiters are used. Instead, the fields are defined by their position within the line:

p-datf13.d
90 Wind Chill Hockey  BBB 
91 Low Key Checkers   DKP 
92 Bing’s Ping Pong   SLS 

In p-datfl3.d, the first three character positions in each line are reserved for the cust-num value, the next 17 positions for the name value, and the last three for the sales-rep value. Space characters may occur between fields, but they may also occur within a field value. To process this file with the IMPORT statement, use the UNFORMATTED option to read one line at a time, as shown in p-impun2.p:

p-impun2.p
DEFINE VARIABLE file-line AS CHARACTER. 
INPUT FROM p-datfl3.d. 
REPEAT: 
   CREATE customer. 
   IMPORT UNFORMATTED file-line. 
   ASSIGN customer.cust-num = INTEGER(SUBSTRING(file-line, 1, 2)) 
          customer.name = TRIM(SUBSTRING(file-line, 4, 17)) 
          sales-rep = SUBSTRING(file-line, 22, 3). 
END. 
INPUT CLOSE. 

After p-impun2.p reads each line, it uses the SUBSTRING function to break the line into field values. It then assigns these values to the appropriate fields in the customer record.

NOTE: If a line in your input file ends with a tilde (~), Progress interprets that as a continuation character. This means, that line and the following line are treated as a single line. Therefore, the IMPORT statement with the UNFORMATTED option reads both lines into a single variable.

What if fields values are separated by a delimiter other than the space character? For example, in p-datfl4.d, field values are separated by commas:

p-datf14.d
90,Wind Chill Hockey,BBB 
91,Low Key Checkers,DKP 
92,Bing’s Ping Pong,SLS 

You could use the UNFORMATTED option of the IMPORT statement to read this file one line at a time and then use the INDEX function to locate the commas and break the line into field values. Another solution is to use the DELIMITER option of the IMPORT statement as shown in p-impun3.p:

p-impun3.p
INPUT FROM p-datfl4.d. 
REPEAT: 
   CREATE customer. 
   IMPORT DELIMITER "," cust-num name sales-rep. 
END. 
INPUT CLOSE. 

In this example, the DELIMITER option specifies that field values are separated by commas rather than by spaces. Therefore, the IMPORT statement parses each line correctly and assigns each value to the appropriate field.

NOTE: You can only specify a single character as a delimiter. If the value you give with the DELIMITER option is longer than one character, then only the first character is used.

For more information on the IMPORT statement, see the Progress Language Reference .


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095