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.

Author
Message
Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 25th Jun 2012 16:29
Hey everybody,

First, don't laugh, I feel embarrassed enough as it is.

I've been working with types and arrays for a few years now but still I don't fully understand them... don't ask my why, I know it's not the most difficult thing to learn...

What if I had this type array thingy



Okay, all good. I add new cabinets (and files) with this command


Still, everything works good. I can search the cabinets in a for-next loop and I can access the data of each cabinet.

Now removing a cabinet is a problem for me. I've tried everything I can imagine.

I figured this should work:


I've tried many other ways, including finding the desired cabinet through a for next loop before removing it..

Removing one, is no problem, but I need to add and remove cabinets constantly but I only get errors ('array out of bound').

Any help is much appreciated! Thanks!!

Regards Sph!nx
www.mental-image.net
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 25th Jun 2012 17:10
Array in DBP start from index zero. And I've never used stacks but the way I do it is like this:



Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jun 2012 20:41
Quote: "I figured this should work:
"


I haven't used stacks but the Help file suggests you can only add/remove the last entry. Perhaps that's your problem?

Anyway, I'd try doing it the way WLGfx suggested.
Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 25th Jun 2012 20:54
Thanks guys.

@ WLGfx:
I'll take another look on the necessity of the stacks. Your example makes sense and should work for me. Thanks.

@ Green Gandalf:
I think that can be the problem yeah.

Regards Sph!nx
www.mental-image.net
Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 25th Jun 2012 22:44


I still get the errors. I added new elements the way WLGfx suggested, works fine btw!

However, still I get the array out of bounds errors. Here is part of my networking code. Help....



Regards Sph!nx
www.mental-image.net
Brendy boy
20
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 25th Jun 2012 22:55
Quote: "However, still I get the array out of bounds errors"

you need to delete elements from the end of the array, not from the begining

Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 25th Jun 2012 23:00
Thanks, but if I need to delete an element that is not at the end of the array? That is the case in my app.

Regards Sph!nx
www.mental-image.net
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jun 2012 23:02 Edited at: 25th Jun 2012 23:29
Why?

Edit Question directed at Brendy Boy. I didn't see your post.
Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 25th Jun 2012 23:05
I keep incoming clients in an array, so when a client left the server, I need to remove that one from the array, no matter what it's position in the list is.

Like I said in the first post, I don't fully understand arrays...

Regards Sph!nx
www.mental-image.net
Brendy boy
20
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 25th Jun 2012 23:14
when you delete an array element the element count gets decreased by 1 so after you delete first element the second element will become first, third will becode 2nd etc.

you have this in your code "for IDz = 0 to total_countz"
When you delete first element, element with index total_countz doesn't exist anymore ->you get an error if you try to delete it and that's exactly what you are doing

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jun 2012 23:33 Edited at: 25th Jun 2012 23:38
Quote: "When you delete first element, element with index total_countz doesn't exist anymore ->you get an error if you try to delete it and that's exactly what you are doing"


That's what I first thought when I read his code. Then I read it again. He's only deleting one item when he's found it - at least that's what he intended I think. Perhaps "goto" is not the way to exit cleanly?

Edit Checking floats for exact equality is a bad idea but I doubt that's the cause of the problem here.
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 25th Jun 2012 23:33 Edited at: 25th Jun 2012 23:38
If you have 10 elements in an array then your range goes from 0 to 9.

If you delete any element, it doesn't really matter which one really, but, you will then have your array elements going from 0 to 8 instead.

In your loop, you are deleting an element but not accounting for the shrinking in size of your array.

I've found in the past that if I were to be deleting array elements in a loop then I run my loop backwards, which then even after deleting an array element, the next loop counter will be minus 1 anyway so it won't be caught out by the new arrays size...

ie:



EDIT: Deleting an array element will shift all the elements beyond that point back one space in the array.

EDIT2: Also to note that in your snippet above, you're getting the size of the array in something_countz, but in your loop you're going from 0 to something_countz. That will go out of bounds unless you change it to something_countz-1. But still when removing elements, check out the snippet I've posted in this post...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jun 2012 23:40 Edited at: 25th Jun 2012 23:50
Well spotted. That sounds like the problem. You'll get the error as soon as you try to delete the final item in the array I guess.

Edit Er, no, that isn't the problem. This is what the Help file says about Array Count():

Quote: "This is usually the value used to create the array when the DIM command was used"


I've just tested the "failing" snippet and it works. I think the simple solution is to print out the index being deleted one at a time and see where the code fails.
Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 26th Jun 2012 15:47 Edited at: 26th Jun 2012 16:19
Thanks everybody!!

@WLGfx:
I have the adding and removing of elements exactly as in you example. Works great.

I understand now... I think. I did know the for-next loop gets broken when the element is removed (for-next is one more than the actual array count) but the goto command stops the for-next loop, so that problem is avoided.

I see now that I have taken the array count right, but starting at 0, instead of 1 makes it +1 and I think that is where my error is. I'll try to fix it. I'll let you guys know how things turn out!

Edit:
Well, still the error happens, but only when the last client leaves the server. This is my updated array code. I've added comments on every line so you know why I did something.


Regards Sph!nx
www.mental-image.net
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 26th Jun 2012 21:13


Also, if you notice that some of your variable are using the # which is for floating point variables. For general loops (that are not using floating point precision) just stick without the # so that they are integers.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 26th Jun 2012 22:15
Thanks again, Tried to implement it but it still gives me the error. This makes me believe the source of the error is elswhere but is triggered by the fact the client is out of the array... so I need to do some hunting in my code...

I'll let you guys know how things turn out.

Regards Sph!nx
www.mental-image.net
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 26th Jun 2012 23:45
Preferably you don't want to constantly create and delete array elements, as it's a slow process especially when the array is big. What is best (and should do) is to pre-define the array to a certain max size and use linked lists (parent child relationship structure) to navigate the active elements stored on the array which is far more efficient, especially when it comes to creating and deleting items via linked lists.


Join or Support!
Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 27th Jun 2012 00:05 Edited at: 27th Jun 2012 00:06
Yeah, that's my plan B. I thought it would be better to remove the element so it would not use memory or something.

Regards Sph!nx
www.mental-image.net
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 27th Jun 2012 00:23 Edited at: 27th Jun 2012 00:23
Quote: "I thought it would be better to remove the element so it would not use memory or something."


But constantly having to reallocate memory is what's slow. Hence why pre-defining is best. Obviously depending on the size of the project you can stick with deleting elements. But it's good practice to do cause you want every one of your app's to be running as efficiently as it can.

Also making function set's to do this is better so you can copy and paste into projects saving time.


Join or Support!
Sph!nx
17
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 27th Jun 2012 01:11
Thanks for the tips! I do use functions for routine operations. Saves a lot of code work too.

Regards Sph!nx
www.mental-image.net

Login to post a reply

Server time is: 2026-07-08 01:56:22
Your offset time is: 2026-07-08 01:56:22