TAKE stage v1.1

Pipelines v2.1

 

Purpose, Operands, Streams, Usage, Examples, Related

Home

 

Syntax

 

            ┌─FIRST─┐  ┌─1───────┐
>>──TAKE──┬─┼───────┼──┼─────────┼──┬───────┬─┬──────────────────────────────────────><
          │ └─LAST──  numrecs─┘  BYTES
                                   └─CHARS─┘ │
          └─┬─ALL─┬───────────────────────────┘
            └─*───┘

 

Purpose

 

Use the TAKE stage to select one or more records from the beginning or end of its primary input stream. TAKE copies the selected records to its primary output stream. If its secondary output stream is connected, TAKE copies all other records to its secondary output stream, otherwise it discards the unselected records.

 

Operands

 

FIRST

select records from the beginning of the input stream. This is the default.

 

LAST

select records from the end of the input stream.

 

ALL

*

specifies that all input records are selected and written to the primary output stream.

 

numrecs

is an unsigned integer which specifies the number of records to be selected from the input stream. The default is 1. If you specify 0 for numrecs, TAKE copies all of its input records to its secondary output stream, if it is connected, otherwise TAKE discards the records and the stage ends.

 

BYTES

Specifies that the number of bytes is counted, rather than records. In general, the specified number of bytes will be taken in the middle of a record, which is then split after the last byte. When FIRST is specified the first part of the split record is selected and the remainder is discarded. When LAST is specified, the first part of the split record is discarded and the second part is selected.

 

Streams used

 

The following streams are used by the TAKE stage:

 

Stream

 

Action

 

 

Primary input stream

TAKE reads records from its primary input stream.

Primary output stream

TAKE copies the selected records to its primary output stream.

Secondary output stream

TAKE copies the unselected records to its secondary output stream, if it is connected.

 

Usage notes

 

1.

TAKE FIRST does not delay the records. TAKE LAST delays the specified number of records.

 

2.

TAKE LAST stores the specified number of records in a buffer. For each subsequent input record (if any), TAKE LAST writes the record residing longest in the buffer to the secondary output stream, if it is connected. Otherwise, it discards the record. The input record is then stored in the buffer, queued on a first-in, first-out basis. When end-of-file is reached on the primary input stream, TAKE LAST writes the specified number of records from the buffer to the primary output stream, if it is connected, otherwise it discards the records.

 

3.

If the TAKE stage discovers that all of its output streams are not connected, the TAKE stage ends.

 

4.

TAKE FIRST disconnects its primary output stream before it writes the unselected records to its secondary output stream. TAKE LAST disconnects its secondary output stream before writing the records to its primary output stream.

 

5.

TAKE verifies that its secondary input stream is not connected and then begins execution.

 

Examples

 

1.

The following pipeline reads the file in.txt and displays on the console only record number 10.

 

   **** Top of file ****
 1 Address Rxpipe
 2
 3 'pipe < in.txt',
 4    '| drop first 9',
 5    '| take 1',
 6    '| console'
 7
 8 Exit 0
   **** End of file ****

 

2.

To display on the console the last 5 lines of a file; you might use the following pipeline:

 

'pipe < myfile.txt | take last 5 | console'
 

This approach, although using only a small amount memory (enough to handle a working queue of up to 5 records), requires the entire input file to be read, record by record, until the FILEIN stage reaches the end-of-file on its input. For small files this may not be an issue, however; for large files, this might require a significant amount of I/O and time needed to traverse the entire input file. A better approach might be to start reading the file in reverse record order, starting at the end-of-file. The following example: Reading a file backwards does exactly that.

 

Related

 

DROP, FROMLABEL, HOLE, TOLABEL

 

History

 

Version

 

Date

Action

Description

Pipelines

1.1

??.??.2025

changed

Application-wide rewrite.

2.1

1.0

06.09.2007

created

First version.

1.0