|
Page 1 of 1
|
[ 9 posts ] |
|
Is there an easy way to remove entires from an array?
| Author |
Message |
|
Grey Gamer
Ensign
Joined: Sun Mar 06, 2011 12:22 am Posts: 205
|
 Is there an easy way to remove entires from an array?
For some reason I keep thinking of "delete row" from Excel, where I could delete one entry and everything would shift over, or would I need to create an algorithm to sort them? Thanks!
_________________ Photons away!
|
| Wed May 02, 2012 12:56 pm |
|
 |
|
Vid Kid
Commander
Joined: Sun Feb 25, 2001 3:00 am Posts: 1838 Location: Guam USA
|
 Re: Is there an easy way to remove entires from an array?
to delete an array value you would
Set the array index value to zero
SetVar $array[$i] 0
then when your reading , always skip if ($array[$i] = 0)
its that or make a new indexed array each run.
_________________ TWGS V2 Vids World on Guam Port 2002 Telnet://vkworld.ddns.net:2002 Discord @ DiverDave#8374 Vid's World Discord
Founding Member -=[Team Kraaken]=- Ka Pla
 Winners of Gridwars 2010 MBN Fall Tournament 2011 winners Team Kraaken Undisputed Champions of 2019 HHT Just for showing up!
The Oldist , Longist Running , Orginal Registered Owner of a TWGS server : Vids World On Guam
|
| Wed May 02, 2012 5:19 pm |
|
 |
|
Grey Gamer
Ensign
Joined: Sun Mar 06, 2011 12:22 am Posts: 205
|
 Re: Is there an easy way to remove entires from an array?
Perfect! Thank you!
_________________ Photons away!
|
| Wed May 02, 2012 5:31 pm |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
 Re: Is there an easy way to remove entires from an array?
The answer is no, there is no way to remove an entry from an array. If you want the array values to be contiguous, and remove one, then you will have to shift the other values. This is my technique for managing arrays: If I have an array named $ep, then I will use a variable with the same name ($ep) to keep track of the size (or effective size) of that array. So if the $ep array has 30 significant elements, then $ep = 30. If I want to remove element # 12, then I would shift the elements above it down, and decrease the size by 1: Code: setVar $i 12 while ($i < $ep) setVar $ep[$i] $ep[($i + 1)] add $i 1 end subtract $ep 1 Now it should be clear that the actual number of elements in the array didn't decrease, rather the variable I use to track the array size was reduced. This method is advisable so that you can stick with static arrays, for performance reasons. An alternate but slower approach would be to copy the array to a new, smaller array, skipping the deleted element. If you (or others) aren't real sure what I'm doing here, then ask, as this is a good technique to learn, and a good example for better understanding arrays.
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Wed May 02, 2012 8:25 pm |
|
 |
|
Grey Gamer
Ensign
Joined: Sun Mar 06, 2011 12:22 am Posts: 205
|
 Re: Is there an easy way to remove entires from an array?
Thanks, ElderProphet, I had that idea in mind, but my version would probably have been way longer and buggy. I tried what Vid Kid recommended, adding:
setVar $ports_list[$this] FALSE
since the script reads:
if ($ports_list[$this] = TRUE) setVar $next_port $this
As far as I know, it did not make any difference, which is actually a partial success, since most things that I try cause functioning scripts to crash.
If the elements in my array are unsorted, how bad would it be to have something like:
setVar($ports_list[$i] $ep setVar($ports_list[$ep] "" subtract $ep 1
Wow. My brain was asleep all day. I literally accomplished nothing as I spun in circles, but then I came home and seemingly effectively scripted.
Thank you very much for your assistance!
_________________ Photons away!
|
| Wed May 02, 2012 10:13 pm |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
 Re: Is there an easy way to remove entires from an array?
Grey Gamer wrote: Code: setVar($ports_list[$i] $ep setVar($ports_list[$ep] "" subtract $ep 1 I'm not sure what you're trying to accomplish here, but that syntax will not compile. What Vid described would zero the array element's value, but not remove it from the array. That's useful, but I use a lot of arrays where a value of zero is normal and expected. What you describe is having an array with a size of, say, 100: you want to delete element 20, and have everything shift down, and the array size now be 99. What I've outlined is the simplest way to effect this.
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Wed May 02, 2012 10:31 pm |
|
 |
|
Grey Gamer
Ensign
Joined: Sun Mar 06, 2011 12:22 am Posts: 205
|
 Re: Is there an easy way to remove entires from an array?
Okay, okay, it should have been:
setVar $ports_list[$i] $ports_list[$ep] setVar $ports_list[$ep] "" subtract $ep 1
In your example, $ep was the array name, as well as the length of the array, and $i was the value that I wanted to remove. This should copy the last value over the undesirable one, remove the last value, and effectively shorten the array.
I do not know why I had $ep instead of $ports_list[$ep] on that first line. I am really bad about matching up parentheses, quotes, etc.
As I mentioned, this is for unsorted arrays, so if they were in some order, I would need to shift all of them.
As always, thank you for your assistance.
_________________ Photons away!
|
| Wed May 02, 2012 11:13 pm |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
 Re: Is there an easy way to remove entires from an array?
Yes, brilliant.
In theory, if you are using a pointer for the end of the array ($ep in this case), you could leave out the 2nd line. You would need to rely on that pointer though, rather than whether or not the element's value is "". I'll frequently do this for high-performance BFS searches where I initialize a large static array, then use pointers like $top and $bottom to track positions in the array. And if I want to overwrite the array, I don't bother re-initializing it or clearing values, I just set my $top and $bottom variables to zero.
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Thu May 03, 2012 8:19 am |
|
 |
|
Grey Gamer
Ensign
Joined: Sun Mar 06, 2011 12:22 am Posts: 205
|
 Re: Is there an easy way to remove entires from an array?
Thanks for your help!
_________________ Photons away!
|
| Fri May 04, 2012 5:10 am |
|
 |
|
|
Page 1 of 1
|
[ 9 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 11 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|