Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Array /list not deleting properly

Author
Message
Scotty1973
AGK Backer
12
Years of Service
User Offline
Joined: 2nd Jun 2011
Location: Burton-on-Trent, uk
Posted: 7th Nov 2011 18:59
Hi all

It may well be me but I can not get the array list commands to do what I want them to do(or think they should do lol)

This is just a test im doing and I realise i can empty the whole array in one command, but on another project i wont be deleteing all the array.

I expected this little program to delete all the array but it only deletes about half. What am i doing wrong?


Thank in advance for your help.

Scotty



Rem Project: ArrayListTest
Rem Created: Monday, November 07, 2011

Rem ***** Main Source File *****

sync on
SYNC RATE 60

BACKDROP ON
COLOR BACKDROP 0

global dim a()

for x=0 to 20
ARRAY INSERT AT BOTTOM a()
a()=x

NEXT x
pressed =0

do
for x=0 to ARRAY COUNT (a())
text 10,x*20,"Item=" +str$(a(x))
NEXT x

if spacekey()=1 and pressed =0
ARRAY INDEX TO top a()
for y=0 to ARRAY COUNT (a())

if a() =y
ARRAY DELETE ELEMENT (a())

else

NEXT ARRAY INDEX a()
endif
NEXT y

endif


text 300,0,"Array count=" +str$(ARRAY COUNT (a()))
sync
LOOP
end

Attachments

Login to view attachments
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Nov 2011 20:00
It's probably because you use the ARRAY COUNT() command in a FOR/NEXT loop that uses ARRAY DELETE ELEMENT that makes ARRAY COUNT() less as it progresses. To fix it you just need to make a variable that stores the ARRAY COUNT().





Also it's best to use code snips so any indenting is carried over to make it easier to read.

Code snips are like this:
[ code ]
Your code here.
[ /code ]

Remove the spaces between the brackets and we see this:


Scotty1973
AGK Backer
12
Years of Service
User Offline
Joined: 2nd Jun 2011
Location: Burton-on-Trent, uk
Posted: 7th Nov 2011 20:23
Hi

Thank you for your reply, and now i realise where I went wrong.


I do indent my code just somehow when i copied it to the forum it went like that.

Many thanks , can now continue with my main project as I now know how to fix it.

Thank you!

Scotty

Login to post a reply

Server time is: 2024-05-20 07:20:56
Your offset time is: 2024-05-20 07:20:56