How to delete an array item and move post array items? lazarus / 21-General ---------------------------------------------------------------------- MrMaxMusterman · 2021-05-01 21:11 So my question is how to remove an item (in this case a record) from an array and how to then rearrange the array so that there wont be any "gap" left behind. In my usecase, i have a program that has an array of records including strings and integers. All records are then being outputted to a listbox. The user then has the option to click on an item (item.index) and press a  button that should start the deletion process. This is the code that i have so far for this procedure: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TProgram_form.DeleteEntry_ButtonClick(Sender: TObject);var  Entry:integer;begin  for Entry:= Output_ListBox.ItemIndex+1 to Output_ListBox.Items.Count do  //End  begin    myArray[Entry]:=myArray[Entry-1];  end;  updateListBox;   //procedure for outputting the current array to the listboxend; As far as Im concerned the entries coming after the one selected should be "moved" up one position but in this case, all i get is that the item selected (that should be deleted) is now the one overwriting all the entries coming after it. So basically every entry after the one that should be deleted including the one that should be deleted now are the same. Can someone help me with this error / problem? Many thanks! ---------------------------------------------------------------------- kupferstecher · 2021-05-01 21:19 This Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i myArray[Entry]:=DSF[Entry-1];is the wrong way round. Should be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i myArray[Entry-1]:= myAray[Entry]; That moves the value with higher index one position down. ---------------------------------------------------------------------- MrMaxMusterman · 2021-05-01 21:26 ok. thanks for that quick solution. My problem now becomes that the entry that way previously shown is now in the end of the list as an empty but still "configured" item. What i mean by that is that in the case of creating for entries (so records in the array) and then deleting the second one for example, the ones being left over are the first, third and fourth one that now moved to the position 1, 2 and 3 + the deleted one with empty data. How can i now delete this array item that is left over? ---------------------------------------------------------------------- lucamar · 2021-05-01 21:44 Quote from: MrMaxMusterman on May 01, 2021, 11:26:15 pmHow can i now delete this array item that is left over?Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i NewLength := Length(MyArray)-1;SetLength(MyArray, NewLength); But note that the "leftover" shouldn't be the "deleted" one, much less with "empty data", but the old 4th which got just copied over to the 3th position. If it looks "empty" it is because your loop goes up to Items.Count where it should go Items.Count - 1 (remember! it starts at zero), so it's off by one and copying over the 4th a "phantom" 5th. Try like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TProgram_form.DeleteEntry_ButtonClick(Sender: TObject);var  Entry:integer;begin  for Entry:= Output_ListBox.ItemIndex+1 to Output_ListBox.Items.Count-1 do  //End  begin    myArray[Entry]:=DSF[Entry-1];  end;  SetLength(MyArray, Length(MyArray)-1);  updateListBox;   //procedure for outputting the current array to the listboxend; ---------------------------------------------------------------------- MrMaxMusterman · 2021-05-01 22:00 Well... I now got the error "variable identifier expected" after calling out my array in SetLength Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for Entry:= Output_ListBox.ItemIndex+1 to Output_ListBox.Items.Count do   begin  Array[Entry-1]:=Array[Entry];  end;  ArrayLength:=Length(Array)-1;  //Array is an integer variable  SetLength(Array, ArrayLength);  updateListBox;  i defined my array under the "type" section of my unit. my length of the array is also reliant on a constant value. it shouldnt matter too much though cause i already looked at output given by the Length command that turns out to be correct. ---------------------------------------------------------------------- alpine · 2021-05-01 22:04 Use other identifier than Array (as you see it is highlighted as reserved word) ---------------------------------------------------------------------- lucamar · 2021-05-01 22:10 Ok, so it's a fixed length array, isn't it? not a dynamic one. Then you'll always have the same number of items in it, no matter what you do. Only thing you can do, if you want, is (depending on the ... record? definition) to do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {Supposing the array type is defined as: TSomeArrray: array [0..4] of TSomeRecord }MyArray[4] := Default(TSomeRecord);{or, more generally}MyArray[High(MyArray)] := Default(TSomeRecord);or, If you can't use Default(), write some function returning a "canary" empty record with easily testable values (negative integers, empty strings, etc.) and assign its result to that last item on the first deletion on a full array (later ones will just copy it down). ETA: Quote from: y.ivanov on May 02, 2021, 12:04:33 amUse other identifier than Array (as you see it is highlighted as reserved word) Absolutely; either change the name or append an "&" in front and call it "&Array", though I don't recommend this last (it can easily become a maintenace nightmare). Note also that if it's indeed a fixed-legth array you'll receive a "Type mismatch" compiler error on the SetLength() line, since it can only be used with dinamyc arrays. ---------------------------------------------------------------------- Paolo · 2021-05-01 23:43 Quote How can i now delete this array item that is left over? As already said, if the array size is fixed you cannot remove the element but if you use dynamic array you can do simply Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Arraydata: array of TSomedata;...Delete(Arraydata, index, count);  //where index is the element to be removed and count if you want remove more rhan one... ---------------------------------------------------------------------- Warfley · 2021-05-02 00:29 If the order of elements is not required to be static you can use the swap delete technique which is much more efficient: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MyArray[DeleteIndex] := MyArray[High(MyArray)];SetLength(MyArray, Length(MyArray) - 1); Otherwise use the Delete function (intrinsic?) ---------------------------------------------------------------------- Zvoni · 2021-05-03 06:11 Instead of inventing the wheel anew, look into the source for TStringList. IIRC, there is already a procedure/function how to "cut out" an item from within an array using "System.Move" ---------------------------------------------------------------------- mangakissa · 2021-05-03 09:17 You're completly right. Use AddObject to connect e record to a listitem. When removing, the actual record is also removed.