Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Reid G.C.Thinking in PostScript.1990.pdf
Скачиваний:
17
Добавлен:
23.08.2013
Размер:
846.44 Кб
Скачать

Example 11.3: Creating and Filling an Empty Array

10 array dup

/one /one /one (four) /five

6 /seven { 8 (the whole procedure) } 9 10 astore

CONSTRUCTING A STRING

Strings can be constructed in much the same way as arrays. You can create one at run-time in the original input program by using the notation found in Example 11.4 for simple strings or hexadecimal strings (also called hex strings).

Example 11.4: Creating Strings with the Scanner

(This is a string created at run-time; following is a hex string:) <a32989e4ff>

Hexadecimal strings are scanned two bytes at a time to create the single byte represented by the 8-bit number. (One byte of hexadecimal, since it is base 16, provides only 4 bits of data; it requires two bytes of hexadecimal to represent a full 8 bits.) Hex strings are generally used only for image data, strings for Kanji character sets, or other places where full 8-bit string data must be used. The string operator can also be used to create an empty string body, and you can use put or putinterval to put bytes into the string.

MANIPULATING DATA WITH PUT AND GET

Strings and arrays are composite data structures. They are both represented by single objects, but really they are composed of collections of objects. There are a few different ways to read and write the contents of these composite data structures, included in Table 11.1. The fundamental mechanism is the family of put and get operators. We’ll start with the simplest ones.

Chapter 11: CREATING AND MANIPULATING DATA

137

Table 11.1: Operators to put and get Data

Arguments Operator

Action

array

length int

number of elements in array

array index

get any

get array element indexed by index

array index any

put ±

put any into array at index

array index count

getinterval subarray

subarray of array starting at

 

putinterval ±

index for count elements

array1 index array2

replace subarray of array1 starting

 

aload a0...an-1 array

at index by array2

array

push all elements of array on stack

any0 ... anyn-1 array

astore array

pop elements from stack into array

array1 array2

copy subarray2

copy elements of array1 to initial

 

forall ±

subarray of array2

array proc

execute proc for each element of

 

 

array

int

string string

create string of length int

string

length int

returns the number of elements in

 

 

string

string index

get int

get string element indexed by index

string index int

put ±

put int into string at index

string index count

getinterval substring

substring of string starting at index

 

putinterval ±

for count elements

string1 index string2

replace substring of string1 starting

 

copy substring2

at index by string2

string1 string2

copy elements of string1 to initial

 

 

substring of string2

The put operator requires three arguments: the thing you want data put into, the key or position in that composite data structure where you want the data put, and finally the piece of data itself that is to be added (see Example 11.5).

Example 11.5: Anatomy of the put Operator

% data_structure position_key new_data put currentdict /key (value) put

10 array 0 (first element) put (XXXXX) 4 (a) put

The putinterval operator is similar to put, but the piece of data you supply needs to be of the same type as the data structure into which you are putting it. The data gets copied from the supplied data into the destination, rather than putting just one piece of data (see Example 11.6).

138

Chapter 11: CREATING AND MANIPULATING DATA