Top

Name

aset — set array element

Synopsis

aset ( in arg any ,
  in nth integer ,
  in new_elem any );
 

Description

aset sets the nth element of a string, array or vector where nth is a zero-based index. If the first argument is a string, the nth character of string is replaced with the ASCII value given in the third argument elem.

Parameters

arg

A string, array or vector.

nth

Zero-based element index.

nelem

The new element. If arg is a string, its nth element will be replaced by the ASCII value given in new_elem .

Return Values

Aset returns nelem . It modifies its first argument.

Errors

Table 24.4. Errors signalled by aset

SQLState Error Code Error Text Description
22003 SR020 Bad array subscript %d in aset.  

Examples

Example 24.15. Using make_string and aref

Make a string, fill with character sequence from A to Z.

SQL> create procedure
alphabet_string ()
{
  declare _inx integer;
  declare _str varchar;
  _str := make_string (26);

  while (_inx < 26)
    {
      aset (_str, _inx, _inx + 65);
      _inx := _inx + 1;
    }

  return (_str);
}
;

Done. -- 6 msec.
SQL> select alphabet_string ();
callret
VARCHAR NOT NULL
_______________________________________________________________________________

ABCDEFGHIJKLMNOPQRSTUVWXYZ

1 Rows. -- 4 msec.


Example 24.16. Reverse a string using aset

Note that str is modified by aset.

SQL> create procedure
revstr (in str varchar)
{
    declare len, inx1, inx2, tmp integer;

    if (str is null) return (str);

    len := length (str);
    if (len < 2)
      return (str); -- No need for further processing

    inx1 := 0;       -- Index from the left.
    inx2 := len - 1; -- Index from the right.
    len  := len / 2; -- Upper limit for inx1.

    while (inx1 < len)
      {
        tmp := aref (str, inx1);
	aset (str, inx1, aref (str, inx2));
	aset (str, inx2, tmp);
	inx1 := inx1 + 1;
	inx2 := inx2 - 1;
      }
    return (str);
}
;

Done. -- 7 msec.
SQL> select revstr ('repaid'), revstr ('Alli, tapa pulu papatilla!');
callret   callret
VARCHAR   VARCHAR
_______________________________________________________________________________

diaper    !allitapap ulup apat ,illA

1 Rows. -- 11 msec.