| www.ClassicTW.com http://www.classictw.com/ |
|
| Is there an easy way to remove entires from an array? http://www.classictw.com/viewtopic.php?f=15&t=33198 |
Page 1 of 1 |
| Author: | Grey Gamer [ Wed May 02, 2012 12:56 pm ] |
| Post subject: | 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! |
|
| Author: | Vid Kid [ Wed May 02, 2012 5:19 pm ] |
| Post subject: | 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. |
|
| Author: | Grey Gamer [ Wed May 02, 2012 5:31 pm ] |
| Post subject: | Re: Is there an easy way to remove entires from an array? |
Perfect! Thank you! |
|
| Author: | ElderProphet [ Wed May 02, 2012 8:25 pm ] |
| Post subject: | 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. |
|
| Author: | Grey Gamer [ Wed May 02, 2012 10:13 pm ] |
| Post subject: | 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! |
|
| Author: | ElderProphet [ Wed May 02, 2012 10:31 pm ] |
| Post subject: | 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. |
|
| Author: | Grey Gamer [ Wed May 02, 2012 11:13 pm ] |
| Post subject: | 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. |
|
| Author: | ElderProphet [ Thu May 03, 2012 8:19 am ] |
| Post subject: | 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. |
|
| Author: | Grey Gamer [ Fri May 04, 2012 5:10 am ] |
| Post subject: | Re: Is there an easy way to remove entires from an array? |
Thanks for your help! |
|
| Page 1 of 1 | All times are UTC - 5 hours |
| Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |
|