Extract

Syntax

EXTRACT attribute [ MAPPER header ] AS extract_file 
[ RETAINAS { file_name | literal } ]

Parameters

Extract can be abbreviated as EX.

Description

The Extract logic command writes the current values, held in the of attribute, into an extract file.

When you extract a Frame, the Main() method of that frame will be automatically executed prior to the extract. The purpose of this is to execute logic that will populate the attributes of the frame prior to it being extracted.

An extract file can be read by:

When using extract files:

Multiple Extracts

If an extract file has already been accessed by another logic statement, such as a Determine Actual logic statement or Sort method invocation, the Extract logic command appends data to the existing file, unless the Restart logic command is used.

The same attribute extracted to the same file from different frames is not distinguished when the file is read back in. For example, if an attribute Cust.Name appears in both Frame10 and Frame11, and these frames are extracted to A_File, a subsequent Determine Actual A_File ExtractedAs Frame10 logic statement and a reference to A_File.Cust.Name refers to extract file records extracted from both Frame10 and Frame11.

Examples

Example 1

Using a Frame “aStruct” you can write the values of the attributes to a file on disk. This logic must be executed as part of a report cycle, otherwise the file will not be closed (see Close() that allows this logic to exist anywhere).

aStruct.Name    := "<Name>" 
aStruct.Address := "<Address>" 
aStruct.Phone   := "<Phone_Number>"  

SetTitle outFile   ("c:\\temp\\struct.txt") 
Extract aStruct As outFile 

Example 2

:   Selected CustInv records are extracted to A_File
:   A_File is sorted on attributes Customer, Product, Trandate and Trantime
:   The contents of A_File are printed to Frame1
:   xEvent is an attribute under A_File templating from the Event EventSet

Determine Actual A_file.xEvent.INVENTORY :sale records for customer, not invoiced
      If A_file.xEvent.AMOUNT > 1000
            Extract A_File.xEvent as A_File
      End
End

A_File.Sort(key(A_File.xEvent.CUSTOMER, "Ascending"),\
			             key(A_File.xEvent.PRODUCT, "Ascending"),\
            key(A_File.xEvent.TRANDATE, "Ascending"),\
			             key(A_File.xEvent.TRANTIME, "Ascending"))

Determine Actual A_File
      Frame1.Customer := A_File.xEvent.CUSTOMER
      Frame1.Product  := A_File.xEvent.PRODUCT
      Frame1.Amount   := A_File.xEvent.AMOUNT
      Frame1.TranDate := A_File.xEvent.TRANDATE
      Frame1.TranTime := A_File.xEvent.TRANTIME
      Frame1.Print()
End 

Example 3

:   For each customer in CUST, extract CustInv records to B_File using Frame 21
:   B_File is sorted on attributes Customer
:   The contents of B_File are printed to Frame22
SetTitle B_File "CUSTTRANS"

LookUp every CUST
      Move glb.ZEROS CustBalance
      Move CUST.CUSTOMER CustId
      Determine from Event.CUSTINV (CustId)
            If Event.CUSTOMER <> CustID
                 Break
            End
            CustBalance := custBalance + Event.AMOUNT
            B_File.Frame21.Customer := Event.CUSTOMER
            B_File.Frame21.PBalance1 := CustBalance
            Extract B_File.Frame21 as B_File
      End
End 

B_File.Sort(key(B_File.Frame21.Customer, "Descending"))

Determine actual B_File
      Frame22.Customer := B_File.Frame21.Customer
      Frame22.PBalance2 := B_File.Frame21.PBalance1
      Frame22.Print()
End

Example 4

This example illustrates that assignment of the name of the extract file by the RetainAs command option is dynamic for Windows, but is declarative for MCP.

For a Windows application, the RetainAs command option determines the filename to be assigned to the extract file at the time of the Extract command’s execution.

For an MCP application, the assignment of the name of the extract file is always that of the last RetainAs command option specified in the current report.

If Field1 = "A"
      Extract Frame01 As A_File RetainAs "AA"
End
If Field1 = "B"
      Extract Frame01 As A_File RetainAs "BB"
End
If Field1 = "C"
      Extract Frame01 As A_File
End

For Windows,

For MCP,

To write code that is compatible with both MCP and Windows, use one of these two alternatives.

Alternative 1: SetTitle command

BeginCase Field1
      Case "A", "B"
            SetTitle A_File Field1 & Field1
            Extract Frame01 As A_File
      Case "C"
            Extract Frame01 As A_File
EndCase

Alternative 2: File.Open() Method

BeginCase Field1
      Case "A", "B"
            A_File.Open (Field1 & Field1, 1)
            Extract Frame01 As A_File
      Case "C"
            Extract Frame01 As A_File
EndCase

Note: In Alternative 2, extract file A_File is opened in write mode. If you want to open the file in append mode, use the value 2 for the second parameter in the A_File.Open() method.

Example 5

This example illustrates how an extract file that has different record formats (such as header, trailer, and data records) can be read.

Note: For MCP, it is necessary for extract file C_File to have instances of Frame09 and Frame10 as members. This is what allows the different record formats to be read from the same file. This restriction makes the ExtractedAs command option redundant in MCP. The ExtractedAs command is redundant if the extract file has instances of all the frames and classes that will be read by the extract file.

Frame09

Attribute

Length

Template

Description

F09_RecTyp

1

String

Record Type

F09_Data1 

3

String

Customer Reference

F09_Data2 

20

String

Customer Name

Frame10

Attribute

Length

Template

Description

F10_RecTyp

1

String

Record Type

F10_Data1 

6

String

Transaction Reference

F10_Data2 

20

String

Transaction Name

Extract Frame09 As C_File
Extract Frame10 As C_File

Determine Actual C_File 
      If C_File.Frame09.F09_RecTyp = "C"                        : Customer Record
            Message C_File.Frame09.F09_Data2  "- Customer Name" : Process customer record
      End
      If C_File.Frame10.F10_RecTyp = "T"                        : Transaction Record
            Message C_File.Frame10.F10_Data2  "- Transaction Amount"
            : Process transaction record
      End
End