HP 3000 Manuals

String Procedures [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation


HP Pascal/iX Reference Manual

String Procedures 

HP Pascal supports a number of string procedures that manipulate string
expressions, variables, or literals.  A string expression may consist of
a string literal, a string variable, a string constant, a function result
that is a string, or an expression formed with the concatenation
operator.

Note that strings must be initialized just like any other variable.  The
string procedures are setstrlen, strappend, strdelete, strinsert,
strmove, strread, and strwrite.  These procedures are described in the
following pages.

setstrlen 

Usage 

     setstrlen(s, e) 

Parameters 

s          A string variable.

e          An integer expression.  The value of e must not be greater
           than the maximum length of s.

Description 

The procedure setstrlen(s, e) sets the current length of s to e without
modifying the contents of s.

If the new length of s is greater than the previous length of s, the
extra components become defined, but no value is given to them.  No blank
filling occurs.  If the new length of s is less than the previous length
of s, previously defined components beyond the new length become
undefined.

Example 

     VAR
        alpha: string[80];

     BEGIN
        .
        alpha:= 'abcdef';                  { strlen(alpha) = 6       }
        .
        setstrlen(alpha,2*strlen(alpha));  { Doubles current length  }
        .                                  { of alpha. Alpha[7]      }
        .                                  { through alpha[12] have  }
        .                                  { unpredictable values.   }
        .
        setstrlen(alpha,2)                 { Alpha[3] through        }
        .                                  { alpha[80] not undefined.}
     END.

strappend 

Usage 

     strappend(s1, s2) 

Parameters 

s1         A string variable.

s2         A string expression whose length must be less than the
           difference between the maximum and actual length of the string
           variable s1.

Description 

The procedure strappend(s1, s2) appends string s2 to s1.  It is an error
if the strlen of s2 is greater than strmax(s1)-strlen(s1).  That is, it
cannot exceed the number of characters left to fill in s1.  The current
length of s1 is updated to strlen(s1)+strlen(s2).

Example 

     VAR
        message: string[132];

     BEGIN
        .
        message:= 'Now hear ';
        strappend(message,'this!');   { message is 'Now hear this!' }
        .
     END.

strdelete 

Usage 

     strdelete(s, p, n) 

Parameters 

s          A string variable.

p          An integer expression representing the starting index of the
           deletion.

n          An integer expression representing the number of characters to
           be deleted.

Description 

The procedure strdelete(s, p, n) deletes n characters from s starting at
component s [ p ] , and the current length of s is updated to the length
of s-n.  It is an error if n+p-1 is greater than the current length of s.

Example 

     VAR
        uncensored, censored: string[80];

     BEGIN
        .
        uncensored:= 'Attack at 6 a.m.!';
        strdelete(uncensored,7,strlen(uncensored)-7);

        censored:= uncensored;  { censored is 'Attack!'. }
        .
        .
     END.

strinsert 

Usage 

     strinsert(s1, s2, p) 

Parameters 

s1         A string expression.

s2         A string variable.

p          An integer or an integer expression representing the offset in
           s2 where insertion begins.

Description 

The procedure strinsert(s1, s2, p) inserts string s1 into s2 starting at
s2 [ p ].  Initially, s2 must be at least p-1 characters in length, or it
is an error.  The resulting string may not exceed strmax(s2).  The
current length of s2 is updated to strlen(s1)+ strlen(s2).

Example 

     VAR
        remark: string[80];

     BEGIN
        .
        remark:= 'There is missing!';
        strinsert(' something',remark,9);{ remark is 'There is something missing! }
        .
     END.

strmove 

Usage 

     strmove(n, s1, p1, s2, p2) 

Parameters 

n          An integer expression indicating the number of characters to
           be copied.

s1         A string expression or PAC variable.

p1         An integer expression indicating the index in s1 from which
           copying starts.

s2         A string or PAC variable.

p2         An integer expression indicating the index in s2 where copying
           starts.

Description 

The procedure strmove(n, s1, p1, s2, p2) copies n characters from s1,
starting at s1[p1], to s2, starting at s2[p2].  The string length of s2 
is increased, if needed, to (p2+n-1) if (p2+n-1) > strlen(s2).  If p2 
equals strlen(s2)+1, strmove is equivalent to appending a subset of s1 to
s2.  It is an error if p2 > strlen(s2)+1.  The value (p1+n-1) must not
exceed strlen(s1).

The strmove procedure may be used to convert PAC's to strings and vice
versa.  It is also a way of manipulating subsets of PAC's.


NOTE The strmove procedure should not be used to move data into an uninitialized variable, regardless of type.

NOTE The strmove procedure is not appropriate for propagating characters within a string. Use the strrpt function or the fast_fill procedure instead.
Example VAR pac: PACKED ARRAY[1..15] OF char; s: string[80]; BEGIN s:= ''; pac:= 'Hewlett-Packard'; strmove(15,pac,1,s,1); { Converts a PAC to a string. } END. strread Usage strread(s, p, t, v) strread(s, p, t, v1,...,vn) Parameters s A string expression. p An integer expression. t An integer or integer subrange variable. v A simple, string, or PAC variable. Any number of v parameters may appear separated by commas. Description The procedure strread(s, p, t, v) reads a value from s, starting at s [ p ] , into the variable v. After the operation, the value of the variable appearing as the t parameter will be the index of s immediately after the index of the last component read into v. S is treated as a single-line textfile. Strread(s, p, t, v) is analogous to read(f, v) when f is a textfile of one line. Like read, strread implicitly converts a sequence of characters from s into the types integer, real, longreal, Boolean, enumerated, PAC, or string. It is an error if strread attempts to read beyond the current length of s. The call: strread (s,p,t,v1,...,vn); is equivalent to: strread (s,p,t,v1); strread (s,t,t,v2); . . strread (s,t,t,vn); Example VAR s: string[80]; p,t: 1..80; m,n: integer; BEGIN . s:= ' 12 564 '; . p:= 1; strread(s,p,t,m); { The value of m will be 12; } . { t will be 6. } . strread(s,t,t,n); { The value of n will be 564; } . { t will be 11. } END. strwrite Usage strwrite(s, p, t, e) strwrite(s, p, t, e1,...,en) Parameters s A string variable. p An integer expression. t An integer or integer subrange variable. e A simple or string expression, or a PAC variable. Any number of e parameters may appear separated by commas. Description The procedure strwrite(s, p, t, e) writes the value of e on s starting at s [ p ] . After the operation, the value of the variable appearing as the t parameter is the index of the component of s, immediately after the last component of s that strwrite has accessed. S is treated as a single-line textfile. Strwrite(s, p, t, e) is analogous to write(f, e) when f is a one-line textfile. As with write, strwrite also permits you to format the value of e as it is written to s using the formatting conventions. The same default formatting values hold for strwrite. Strwrite may write into the middle of a string without affecting the original length. It is an error if strwrite attempts to write beyond the maximum length of s, or if p is greater than strlen(s)+ 1. The current length of s is updated if the current length is increased. Example VAR s: string[80]; p,t: 1..80; f,g: integer; BEGIN f:= 100; g:= 99; p:=1; s:=''; { empty string } . strwrite(s,p,t,f:3); { S is now '100'; t is 4 } strwrite(s,t,t,' ',g:2); { S is now '100 99'; t is 7. } . END.


MPE/iX 5.0 Documentation