Create selection from array

The “Create selection from array” command is working mysteriously with me. It retrieves records, but with an ID greater by one to the original.
For instance, when running the following :

ARRAY LONGINT ($TabUserID;0)
APPEND TO ARRAY ($TabUserID;1)
APPEND TO ARRAY ($TabUserID;3)
APPEND TO ARRAY ($TabUserID;5)
CREATE SELECTION FROM ARRAY ([User];$TabUserID)

I retrieve three records with IDs 2, 4 and 6 (instead of 1,3 and 5).
Am I missing something ?
Thanks for your help.

Can you try this:

ARRAY LONGINT($rL_Sel;0)
LONGINT ARRAY FROM SELECTION([CONTRAT];$rL_Sel)

REDUCE SELECTION([CONTRAT];0)
CREATE SELECTION FROM ARRAY([CONTRAT];$rL_Sel)

This syntax uses the record number, which starts from 0. The auto-incremental integer primary key (field named “ID” by default) starts from 1.

Hi Antoine,
to get your code working, simply replace last line:

ARRAY LONGINT ($TabUserID;0)
APPEND TO ARRAY ($TabUserID;1)
APPEND TO ARRAY ($TabUserID;3)
APPEND TO ARRAY ($TabUserID;5)
QUERY WITH ARRAY ([User]ID;$TabUserID)

It’s similar to

QUERY ([User];[User]ID;1;*)
QUERY ([User];|;[User]ID;3;*)
QUERY ([User];|;[User]ID;5)

As Miyako said, CREATE SELECTION FROM ARRAY is designed to get records from their “position” in the table. If a record is deleted, that position becomes free for a new one added after. This command is very fast but should be used only for temporary purpose, because the record reached this way can change. Most of the time, you will use CREATE SELECTION FROM ARRAY with an array created before with LONGINT ARRAY FROM SELECTION

Thank you Arnaud, I understand your explanations.
I assume that Query With Array is not very fast and shouldn’t be used with large arrays, right?

A.

Thanks Miyako, that makes a lot of sense.

A.

Thank you Bertrand. I didn’t realize that create selection from array is intended to recreate a previous selection.

A.

For sure, the pair LONGINT ARRAY FROM SELECTION / CREATE SELECTION FROM ARRAY is faster, but I wouldn’t say that QUERY WITH ARRAY is slow: use it when you need it and for what it is made for… If it happens to be too slow, it will be time to see.

1 Like