Recent

Author Topic: Delete Array Item (multidimensional)  (Read 13825 times)

LazaruX

  • Hero Member
  • *****
  • Posts: 597
  • Lazarus original cheetah.The cheetah doesn't cheat
Delete Array Item (multidimensional)
« on: March 09, 2009, 03:28:19 pm »
I am using a muiltidimensional array (2D) to keep some data in memory.
This is an example that explaines it
Code: [Select]
Table:array of array of string; // This is a bidimensional array


Table[X][Y]:='Lazarus';
Table[5][3]:='O';

    1234 67890.....
   -----5----------------- x
  1|    |
  2|    |
   3----O
  4|
  5|
  6|
   y
   

   
What I want to do is be able to shit some items up in the array.
So when I delete what is on Table[5][2] then item Table[5][3] should move up and go to position Table[5][2]

Can somebody help me in telling me how to do that?


Legolas

  • Full Member
  • ***
  • Posts: 117
    • http://itaprogaming.free.fr
Re: Delete Array Item (multidimensional)
« Reply #1 on: March 09, 2009, 04:03:55 pm »
Maybe you could use an array of TStringList, where the stringlist contains the columns. Keep an eye on the list length, because it is easy to access items out of bounds

LazaruX

  • Hero Member
  • *****
  • Posts: 597
  • Lazarus original cheetah.The cheetah doesn't cheat
Re: Delete Array Item (multidimensional)
« Reply #2 on: March 09, 2009, 08:42:01 pm »
Grazie,
but If I need to do what I explained above with integers and reals???

I know I could save the values as string and convert them, but is there an equal as TstringList for reals?
If not I will use your suggestion

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11923
  • Debugger - SynEdit - and more
    • wiki
Re: Delete Array Item (multidimensional)
« Reply #3 on: March 09, 2009, 09:20:44 pm »
Tricky,

If you only need that for the 2nd (Y) axis, then look at Move (from the System unit). It saves you writing loop and copy every element.

A word of warning. You can use Move if the values are int or real. (Make sure you stik to the boundaries => there is no check). Don't forget to set the last value to whatever it should be.

Don't do it if your values are strings, or Variable-length Array. Those are ref-counted, and unless you know exactly what you do, you are bound to end up somewhere between mem-leaks and crashes.

You can always just write a for ... to .. do loop and copy all elements up


arnoldb

  • Jr. Member
  • **
  • Posts: 97
Re: Delete Array Item (multidimensional)
« Reply #4 on: March 10, 2009, 07:03:49 am »
You need to know the maximum dimensions of your array (the sizes you set it to using setlength(MyArray,maxx,maxy))

to move up to delete a "cell" at [x,y]:

var i:integer

for i := y to maxy -2 do
  MyArray[x,i]:=MyArray[x,i+1];

MyArray[x,maxy -1] := '';


Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Delete Array Item (multidimensional)
« Reply #5 on: March 10, 2009, 11:33:33 am »
I prefer Martin_fr's suggestion for this, System.Move is MUCH faster.

Legolas

  • Full Member
  • ***
  • Posts: 117
    • http://itaprogaming.free.fr
Re: Delete Array Item (multidimensional)
« Reply #6 on: March 10, 2009, 11:35:29 am »
Grazie,
but If I need to do what I explained above with integers and reals???

I know I could save the values as string and convert them, but is there an equal as TstringList for reals?
If not I will use your suggestion

In this case, you could use a simple TList

 

TinyPortal © 2005-2018