Remove first and last record

Hello developers, i want to remove first and last record from list variable. Is it possible to do it?

Hi,


Use below function to delete record from List,


Also you can refer below link to understand list operation,

https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Data/Data_Types/List

And to avoid stupid mistakes: if you remove the first, the last will change position so careful and don't save the position before deleting to then be surprised at not finding it.


Yes, I agree with Nuno.

Nuno Reis wrote:

And to avoid stupid mistakes: if you remove the first, the last will change position so careful and don't save the position before deleting to then be surprised at not finding it.


If you do in the same call to the server without committing,  the last position should be the same, so remove all in the same request,  that is right Nuno or am I wrong?


Sorry Carlos, I think you're are not correct here.

The positions are relative to the elements currently in the List.
If you have 10 elements, the last element "position" is 9. If you remove the first element, the last element position is now 8. 

Cheers.

P.S. The "effect" is immediate. No related to transaction/request. 

Server and commits are not relevant here. This is a local operation. 

Removing the later positions first is a good principle, even if there are exceptions (and I can't think of one).


When I remove from a list while in a loop, the object is changed and length changes immediately.


And if a commit was needed, could you alter two elements in one operation? +


Thanks Edu and Nuno for this clarification

Thanks all of you for the fast reply, and i have one more question. As describe in picture, i use the list remove to delete first record as JitendraYadav said. But how can i find the last position of last record?

Solution

Hello,

Like it was said before, the ListRemove action removes a record from the list with a given position.

If the list has N elements, then the first position will be 0 and the last position will be N-1.

Example: list will 10 elements -> [0][1][2][3][5][6][7][8][9]


In order to remove the first position of the list, it is exactly like you did (position 0 of the list).

In order to remove the last position of the list, you can use the length property of the list minus 1. The length property will give you the number of elements currently in the list. If you subtract 1 to this number, then you will have the last position of the list.

In the example above, this is the scenario:

  • List with 10 elements -> remove first: position 0 -> list with 9 elements
  • List with 9 elements -> remove last: position lenght - 1 -> list with 8 elements


These are the Runtime Properties that you can use in your code for a list:

https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Data/Data_Types/List


Hope it helps.


Kind regards,

Rui Barradas

Solution

Rui Barradas wrote: Thank you for your replay Rui, it actually work for me.

Hello,

Like it was said before, the ListRemove action removes a record from the list with a given position.

If the list has N elements, then the first position will be 0 and the last position will be N-1.

Example: list will 10 elements -> [0][1][2][3][5][6][7][8][9]


In order to remove the first position of the list, it is exactly like you did (position 0 of the list).

In order to remove the last position of the list, you can use the length property of the list minus 1. The length property will give you the number of elements currently in the list. If you subtract 1 to this number, then you will have the last position of the list.

In the example above, this is the scenario:

  • List with 10 elements -> remove first: position 0 -> list with 9 elements
  • List with 9 elements -> remove last: position lenght - 1 -> list with 8 elements


These are the Runtime Properties that you can use in your code for a list:

https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Data/Data_Types/List


Hope it helps.


Kind regards,

Rui Barradas



No problem mate, I'm glad that you managed to do it.


Kind regards,

Rui Barradas