insert()

Read(3294) Label: insert,

Here’s how to use insert() functions.

A.insert()

Description:

Insert members into a sequence.

Syntax:

A.insert(k,X)

Insert the members of sequence X before position k in A, and return A

A.insert(k,x)

Insert member x before position k in A, and return A

Note:

The function inserts member x or members of sequence X before position k in sequence A. When parameter k is omitted, we assume the sequence A is ordered and insert member X(or x); if member X(or x) have already existed in A, the function won’t perform the insertion. Automatically update the index, if any, and check distinctness.

Parameter:

k

The position before which one or more members are inserted, when k==0, the member(s) will be appended in the end; insert backward from the end when k is less than 0

A

A sequence

X

A sequence composed of the members to be inserted

x

A member

Option:

@n

Return the inserted record or a record sequence of the inserted records

Return value:

Sequence

Example:

 

A

 

1

=["a","c","d","e","f"]

 

2

=A1.insert(0,"g")

[a,c,d,e,f,g], the member is appended in the end and A1 changes.

3

=A1.insert(2,["g","j"])

[a,g,j,c,d,e,f,g], insert the new members before the second member.

4

=demo.query("select * from STUDENTS")

 

5

=A4.insert@n(2,ID+10:ID,"Lily":NAME,"M":GENDER,15:AGE)

Return the inserted record.

6

=A1.insert(,"b")

[a,b,g,j,c,d,e,f,g]; assume A1 is ordered and insert a new member b.

7

=A1.insert(,"a")

[a,b,g,j,c,d,e,f,g]; member a exists in A1, so the same member won’t be inserted.

8

=A1.insert(-2,"h")

As k<0, insert backwards from the end and return [a,b,g,j,c,d,e,h,f,g].

Related function:

A.delete()

A.modify()

T.insert()

T. insert()

Description:

Insert one or more records into a table sequence.

Syntax:

T.insert(k)

Insert an empty record before the position k in the T. If k is 0, then append it in the end and return the new T

T.insert(k,xi:Fi,…)

Insert a record into T before the position k where the value of Fi is xi and return the new T. When parameter k is omitted, we assume the table sequence T is already ordered by the key and insert records; if the key value has already existed, the function won’t perform the insertion

T.insert(k:A,xi:Fi,…)

Insert multiple records into T before the position k where the value of Fi is xi and return the new T. The number of the records to be inserted is determined by the length of sequence A

Note:

The function inserts one or more records into the table sequence T, and automatically updates the index, if any, and checks distinctness.

Parameter:

k

The position before which the member, or the record, is inserted

xi

The Fi field value before which the new record is to be inserted

Fi

The name of field where xi resides; without Fi, it will be the corresponding ith field.

T

A table sequence

A

A sequence or an integer; if A is an integer, then it is equal to to(A)

Option:

@n

Return the inserted record or a record sequence of the inserted records

@r(k:A)

Insert sequence A into table sequence T from the kth record according to the order of the fields

@f(k:A)

Insert sequence A into table sequence T from the kth record; only the common fields are inserted

Return value:

Table sequence

Example:

 

A

 

1

=create(id,name,age)

Construct an empty table sequence.

2

=A1.insert(0,1,"Jack",29)

Append a record whose field values are 1, Jack, and 29.

3

=A1.insert(1,2,"Lucy",30)

Add a record before the first record whose values are 2, Lucy, and 30.

4

=A1.insert(0)

Append an empty record in the end:

5

=A1.insert(0:3)

Append three empty records in the end:

6

=A1.insert@n(2:1,10,"Lily",30)

Return the inserted record:

7

=create(ID,Name,Age)

 

8

=A7.insert(0:A1,id:ID,name:Name,age:Age)

Add the records of A1 into A7 one by one:

9

=A1.delete(A1.select(id<2))

 

10

=A7.insert@r(5:A9)

Insert A9 into A7 from the fifth record:

11

=create(ID,Name,AGE)

 

12

=A11.insert@f(3:A7)

Insert only the ID and Name fields of A7 to A11 from the third record:

 

13

=A1.keys(id)

Set id as A1’s primary key, then A1’s data is as follows:

14

=A1.insert(,5,"Mary",28)

Parameter k is omitted; A1 is already ordered by id and insert the records.

15

=A1.insert(,2,"CC",32)

Same result as A14; since the record where the key value is 2 already exists, the function cancels the insertion.

16

=file("D:\\test.txt").import@t()

test.txt defines only the column headers.

17

=A16.insert(0,1,"2008-8-4")

Insert a record:

Related functions:

T.modify()

T.delete()

A.insert()